Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.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_Multidimensional Array - Fatal编程技术网

Php 按具有指定键的其他数组分析多维数组

Php 按具有指定键的其他数组分析多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想使用包含键的其他数组来解析主数组 例如,我有以下主阵列: $test_array = [ 'something' => 'Ceva', 'products' => [ 'item' => [ [ 'id' => '1', 'images' => [ [

我想使用包含键的其他数组来解析主数组 例如,我有以下主阵列:

$test_array = [
       'something' => 'Ceva',
       'products' => [
           'item' => [
               [
                   'id' => '1',
                   'images' => [
                       [
                           'image' => 'image1.jpg'
                       ],
                       [
                           'image' => 'image2.jpg'
                       ]
                   ]
               ],
               [
                   'id' => '1',
                   'images' => [
                       [
                           'image' => 'image12.jpg'
                       ],
                       [
                           'image' => 'image22.jpg'
                       ]
                   ]
               ],
           ],
       ]
]
这是包含键的第二个数组:

$settings = [
  'products_path' => 'products,item',
  'images_path' => 'products,item',
];
我需要一种使用设置数组从数组中获取特定路径的方法

example_function($test_array, $settings['products_path']);
应返回:

    [
    'id' => '1',
    'images' => [
        [
            'image' => 'image1.jpg'
        ],
        [
            'image' => 'image2.jpg'
        ]
    ]
],
[
    'id' => '1',
    'images' => [
        [
            'image' => 'image12.jpg'
        ],
        [
            'image' => 'image22.jpg'
        ]
    ]
],
更新

我已使用此功能完成此操作:

public function getPath($array, $columns, $return = []) {
        if(!empty($columns)) {
            foreach ($columns as $column_key => $column) {

                if(isset($array[$column])) {
                    unset($columns[$column_key]);
                    $return = $this->getPath($array[$column], $columns);
                }
            }
        } else {
            $return = $array;
        }

        return $return;
    }

如果这个例子看起来很简单,那么您只需分离$settings键内的值,并将它们用作数组索引

function example_function($test_array, $settings_key_value ) {

    // Separate the two $settings values from the 'product_path' key at the comma, using the first as your first index inside $test array, and second word as second index.
    $settings_value = explode(',', $settings_key_value);
    $new = $test_array[$settings_value[0]][$settings_value['1']];

}
只需调用示例中的函数:

example_function($test_array, $settings['products_path']);
此外,可能有11000个备选方案来获得您的答案。所有的工具都放在那里,如果$settings数组可以稍加修改,你甚至不需要explode()函数就可以实现

编辑:由于每个键中可能有不同数量的值:

function example_function($test_array, $settings_key_value ) {

    $settings_values = explode(',', $settings_key_value);
    foreach ($settings_values as $settings_value ) {
        $output = $test_array[$settings_value];
        if ( is_array( $output )) {
            $test_array = $output;
        }
        break;
    }

    return $test_array;
}

好主意,但是可以使此功能自动工作。设置数组长度可以有更多或更少的键。设置数组中的最后一个值是否始终是搜索的最深级别?我假设你的意思是每个键可以有不同数量的值?是的,设置数组的最后一个值总是最深的数组键