Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-查找多维数组搜索_Php_Arrays - Fatal编程技术网

PHP-查找多维数组搜索

PHP-查找多维数组搜索,php,arrays,Php,Arrays,我有一个多维数组,我需要在其中找到属性名“Gallery”并为此检索值…在我的测试中,这应该是“test_Gallery”。所有这些都使用PHP。请注意,数组中数据的顺序可能会更改,最后一个数组的顺序将始终保持不变…属性\ id、名称、文本 此阵列的结构如所附图像所示 PS:下面是执行相同操作并正常工作的细枝代码: {% for attribute_group in attribute_groups %} {%

我有一个多维数组,我需要在其中找到属性名“Gallery”并为此检索值…在我的测试中,这应该是“test_Gallery”。所有这些都使用PHP。请注意,数组中数据的顺序可能会更改,最后一个数组的顺序将始终保持不变…属性\ id、名称、文本

此阵列的结构如所附图像所示

PS:下面是执行相同操作并正常工作的细枝代码:

                    {% for attribute_group in attribute_groups %}
                      {% for attribute in attribute_group.attribute %}
                        {% if attribute.name == 'Gallery' %}                
                            {gallery}{{ attribute.text }}{/gallery}                             
                        {% endif %}
                      {% endfor %}
                    {% endfor %}
以下是我尝试使用的php代码:

        $arr = $data['attribute_groups']; //my array, print_r values are source of attached image

        foreach ($arr as $attribute_group){

                foreach($attribute_group as $attribute){
                    print('<pre>');
                    print_r($attribute);
                    print('</pre>');
                  if ($attribute['name'] == 'Gallery'){
                    print('<pre>');
                    print_r($attribute['text']);
                    print('</pre>');
                  }
            }
        }
$arr=$data['attribute_groups']//我的数组、打印值是附加图像的源
foreach($arr作为$attribute\u组){
foreach($attribute\u组作为$attribute){
打印(“”);
打印(属性);
打印(“”);
如果($attribute['name']=='Gallery'){
打印(“”);
打印($attribute['text']);
打印(“”);
}
}
}
非常感谢


您可以使用嵌套循环检索它。根据阵列结构的不同,它可以非常灵活地完成

我使用以下代码对其进行了测试,这些代码是根据您所附的图像编写的:

我这样做是为了检索包含您搜索的字符串的组:

测试库


创建嵌套循环。我曾尝试使用foreach两次,但未能使其正确共享代码!巢是保持原样还是可以改变?谢谢你的帮助。我添加了我试图使用的代码。我应该注意到,筑巢不会改变,我有理由要把它从树枝上取下来。这一切都是从Opencart,我需要得到画廊属性文本从树枝到控制器。不用担心@马丁斯捷潘内克

$attribute_groups = [
  4 => [
    'attribute_group_id' => 4,
    'name' => "Multimedia",
    'attribute' => [
        0 => [
          'attribute_id' => 26,
          'name' => "Gallery",
          'text' => "test_gallery",
        ],
      'attribute' => [
        1 => [
          'attribute_id' => 13,
          'name' => "Something",
          'text' => "test_something",
        ],
      ],
    ]
  ]
];
$search = "gallery";
foreach ($attribute_groups as $attribute_group) {
  foreach ($attribute_group['attribute'] as $group) {
    if (key_exists('name', $group) 
        && strtolower($group['name']) == strtolower($search)
    ) {
      print_r($group['text']);
    }
  }
}