I have array which show result like this :
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
Array ( [standards_id] => 1
[value] => javid
[order] => 3
)
Array ( [standards_id] => 1
[value] => saleem
[order] => 4
).
I want to check the array key ,if it is "value" then i want to concatenate its value.I try code like this but not successeded.
$row = array(.....);
$vali = '';
foreach ($row as $key => $value) {
if( $value[$key] == 'value'){
echo $vali .= $value['value'].",";
}
}
I want to do it in one loop.$row contains multiple arrays like above 3.$row contains all the records that are fetched from data base.hope you understand what $row is.
Use isset()
to find out whether the key 'value'
is present in each element of $row
; you don't need a loop for that at all:
foreach ($row as $data) {
if (isset($data['value'])) {
$vali .= $data['value'] . ',';
}
}
Alternatively, you can build an array with the values:
$values = array();
foreach ($row as $data) {
if (isset($data['value'])) {
$values[] = $data['value'];
}
}
echo join(',', $values);
You need this:
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value.",";
}
}
echo $vali;
$row = array('standards_id' => 1, 'value' => 'saleem', 'order' => 4);
$vali = '';
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value . ",";
}
}
echo $vali;
Your naming suggests that $row
contains just:
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
There is no reason to loop over such a structure. You can test if this 'row' contains a value with
if( isset( $row['value'] ) ) {
$vali .= $row['value'] . ', ';
}
If you have a variable $myRows
containing these arrays, you can loop over them using
foreach( $myRows as $k => $row ) { ... }
In this case $row
contains the array and you can use the first code to append the value to $vali
.