Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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_Multidimensional Array_Foreach - Fatal编程技术网

PHP多维数组遍历值

PHP多维数组遍历值,php,multidimensional-array,foreach,Php,Multidimensional Array,Foreach,我想使用foreach(带键和值)遍历多维数组 我的问题是数组并不总是相同的(也就是说,数组是动态的,因为我在响应中收到它们)。如何在循环中遍历它们?以下是阵列结构的示例: 第一个阵列: Array( [id] => [client_customer_id] => [reference] => [client_order_id] => [status] => [name_search_records] => Array ( [

我想使用
foreach
(带键和值)遍历多维数组

我的问题是数组并不总是相同的(也就是说,数组是动态的,因为我在响应中收到它们)。如何在循环中遍历它们?以下是阵列结构的示例:

第一个阵列:

Array(
[id] => 
[client_customer_id] => 
[reference] => 
[client_order_id] => 
[status] => 
[name_search_records] => Array
    (
        [0] => Array
            (
                [id] => 
                [first_name] => 
                [last_name] => 
                [middle_name] => 
                [suffix] => 
                [name_type] => 
                [services] => 
                    (
                        [ucc] => Array
                            (
                                [name_search_record_id] => 
                                [request_header_id] => 
                                [is_at_any_address] => 
                                [status] => 
                                [create_date] => 
                            )

                    )

            )

    )
第二个阵列:

Array(
[id] => 
[client_customer_id] => 
[reference] => 
[client_order_id] => 
[status] => Canceled
[property_search_records] => Array
    (
        [0] => Array
            (
                [id] => 
                [order_detail_id] => 
                [address_line1] => address
                [county_id] => 
                [district_id] =>
                [state_id] => 
                [services] => Array
                    (
                        [flood_lol] => Array
                            (
                                [property_search_record_id] => 
                                [request_header_id] => 
                                [is_lol] => 
                                [lender] => 
                                [lender_address_line1] => 
                                [lender_address_line2] => 
                                [lender_city] => 
                                [lender_zip] => 
                                [status] => 
                                [create_date] => 
                            )

                    )

            )

    )

)

我不确定我是否明白你的问题。您对确定哪个键包含数组而哪个键不包含数组感兴趣吗?在这种情况下,您可以执行以下操作:

function check_arr($arr) {
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            echo $key . ' is array' . '<br>';
            check_arr($value);
        } else {
            echo $key . ' is not array' . '<br>';
        }
    }
}


$arr1 = array(
    'a' => 1,
    'b' => 2,
    'c' => array(
        'd' => 3,
        'e' => array(
            'f' => 4,
            'g' => array(
                'h' => 5,
                'i' => 6,
                'j' => array(
                    'k' => 7,
                    'l' => array(
                        'm' => 8
                    )
                )
            )
        )
    )
);


$arr2 = array(
    'a' => 1,
    'b' => array(
        'c' => 2,
        'd' => 3
    ),
    'e' => 4
);
检查arr($arr2)的输出:

您可以使用数组进行任意深度的操作,并且可以在遇到包含感兴趣数组的键时添加自己的逻辑。这就是你要找的吗

a is not array
b is not array
c is array
d is not array
e is array
f is not array
g is array
h is not array
i is not array
j is array
k is not array
l is array
m is not array
a is not array
b is array
c is not array
d is not array
e is not array