views:

58

answers:

4
array(2) {
  [0]=>
  object(stdClass)#144 (7) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(8) "name1"
    ["value"]=>
    string(22) "Lorem Ipsum Dolar Amet"
    ["type"]=>
    string(8) "textarea"
    ["group"]=>
    string(1) "1"
    ["published"]=>
    string(1) "1"
    ["ordering"]=>
    string(1) "1"
  }
  [1]=>
  object(stdClass)#145 (7) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(6) "Link1"
    ["value"]=>
    string(36) "abcabcab"
    ["type"]=>
    string(4) "link"
    ["group"]=>
    string(1) "1"
    ["published"]=>
    string(1) "1"
    ["ordering"]=>
    string(1) "2"
  }
}

I want to print only "value" (abcabcab) of id=4. How can I achieve this?

A: 
array_walk($a, function($el){if($el->id === 4){print $el->value;}});
Matthew Flaschen
That's PHP 5.3 and up only, and a little... exotic. Hopefully this was a tongue-in-cheek answer? :)
Thorarin
It's nice to take a different approach occasionally. :)
Matthew Flaschen
A: 
foreach ($array as $entry) {
   if ($entry['id'] == 4) 
      echo $entry['value'];

}
ZZ Coder
Since it's an object, object property syntax would be preferable for `$entry->value`
Thorarin
+1  A: 
foreach($array as $row){
 if($row['id']==4){
  print($row['value']);
 }
}
Mike Sherov
Do note that the id is a string. While I do not expect anything strange in conversions here, I prefer to stay away from type conversions for comparison when possible.
Jasper
A: 

this works:

foreach ($array as $entry) {
   if ($entry->id == 4) 
      echo $entry->value;
}

Thanks!

cateye