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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2761508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:36:05  来源:igfitidea点击:

How to print a specific value in array in PHP?

phparraysforeach

提问by cateye

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?

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

采纳答案by ZZ Coder

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

}

回答by Mike Sherov

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

回答by cateye

this works:

这有效:

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

Thanks!

谢谢!

回答by Matthew Flaschen

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