1
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?

4 Answers 4

2
foreach($array as $row){
 if($row['id']==4){
  print($row['value']);
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1
foreach ($array as $entry) {
   if ($entry['id'] == 4) 
      echo $entry['value'];

}

1 Comment

Since it's an object, object property syntax would be preferable for $entry->value
1
array_walk($a, function($el){if($el->id === 4){print $el->value;}});

2 Comments

That's PHP 5.3 and up only, and a little... exotic. Hopefully this was a tongue-in-cheek answer? :)
It's nice to take a different approach occasionally. :)
1

this works:

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

Thanks!

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.