【问题标题】:PHP mutidimensional array traverse valuesPHP多维数组遍历值
【发布时间】:2018-03-15 15:07:29
【问题描述】:

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

                    )

            )

    )

)

【问题讨论】:

    标签: php multidimensional-array foreach


    【解决方案1】:

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

    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
    

    您可以随心所欲地使用数组,并且可以在遇到包含感兴趣数组的键时添加自己的逻辑。这是你要找的吗?

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 2012-04-21
      • 2011-01-02
      • 2014-10-03
      • 2016-02-16
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多