0

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.

8
  • What's the actual content of $row? One row or multiple rows? Commented Jul 11, 2013 at 8:51
  • @Jack $row contains multiple arrays like above 3. Commented Jul 11, 2013 at 8:52
  • @Fasilkk Becoz i want to fetch only value field records. Commented Jul 11, 2013 at 8:54
  • Then why don't you call it $rows instead? Commented Jul 11, 2013 at 8:55
  • Your array is multidimensional?? Commented Jul 11, 2013 at 8:57

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

$row store multi arrays at a time.I cannot check only value like you suggest.
I would suggest to rename that variable then to $rows or $myRows or something that suggests it holds multiple rows. This helps with the readability of the code.
0

You need this:

    foreach ($row as $key => $value) {

            if( $key == 'value'){

                     $vali .= $value.",";
            }
        }

echo $vali;

Comments

-1
$row = array('standards_id' => 1, 'value' => 'saleem', 'order' => 4);
$vali = '';
foreach ($row as $key => $value) {
    if( $key == 'value'){
        $vali .= $value . ",";
    }
}
echo $vali;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.