PHP多维数组遍历值

时间:2018-03-15 15:07:30

标签: php multidimensional-array foreach

我有一个多维数组,我想用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] => 
                            )

                    )

            )

    )

)

1 个答案:

答案 0 :(得分:0)

我不确定我是否接受了您的问题。你是否有兴趣确定哪个键包含一个数组而哪个不包含数组?在这种情况下,你可以这样做:

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
);

check_arr($arr1)的输出:

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

check_arr($arr2)的输出:

a is not array
b is array
c is not array
d is not array
e is not array

您可以使用数组尽可能深入,并且在遇到包含感兴趣数组的键时可以添加自己的逻辑。这是你在找什么?