如何在PHP中打印数组中的特定值?

时间:2010-05-03 22:01:05

标签: php arrays foreach

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"
  }
}

我想只打印id = 4的“value”(abcabcab)。我怎样才能做到这一点?

4 个答案:

答案 0 :(得分:2)

foreach($array as $row){
 if($row['id']==4){
  print($row['value']);
 }
}

答案 1 :(得分:1)

array_walk($a, function($el){if($el->id === 4){print $el->value;}});

答案 2 :(得分:1)

foreach ($array as $entry) {
   if ($entry['id'] == 4) 
      echo $entry['value'];

}

答案 3 :(得分:1)

这有效:

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

谢谢!