如何从数组内部的数组中查找值?

时间:2019-02-05 14:39:06

标签: php

我有一个这样的数组:

Array ( 
[0] => Array (  [attribute_group_id] => 1
                [name] => Name 1 
                [attribute] => Array (  [0] => Array ( 
                                            [attribute_id] => 1 
                                            [name] => Attribute 1
                                            [text] => AAA) 
                                        [1] => Array ( 
                                            [attribute_id] => 2 
                                            [name] => Attribute 2 
                                            [text] => BBB ) 
                                        [2] => Array ( 
                                            [attribute_id] => 3 
                                            [name] => Attribute 3 
                                            [text] => CCC ) 
                                        ) ) )

我只想在这里提取BBB文本,但是如果我构造foreach函数,它会输出类似BBB BBB BBB的内容(如下示例):

foreach ($p_atts as $p_att) {
  foreach ($p_att['attribute'] as $attribute) {
    if ($attribute['attribute_id'] = '2') {
       $out .= $attribute['text'];
    }
  }
}

如果您能帮助我,我会很高兴!

1 个答案:

答案 0 :(得分:1)

在if语句中必须有两个等号。

foreach ($p_atts as $p_att) {
  foreach ($p_att['attribute'] as $attribute) {
    if ($attribute['attribute_id'] == '2') {
       $out .= $attribute['text'];
    }
  }
}