使用未指定的索引数组php

时间:2018-01-15 13:16:51

标签: php arrays associative-array

我有一个关联数组(似乎是)一个动态整数。 数组:

if($_SESSION['session_user_type'] == 'user' && $del_date1 == '' or $del_date1 > $row_cap['cap_date']){}

例如,如果我想获取attendee_firstname,请使用以下内容:

array(4) {
    ["attendees"]=> array(1) {
        [4]=> array(1) {
            [0]=> array(9) {
                ["attendee_firstname"]  => string(6) "Privat"
                ["attendee_secondname"] => string(6) "Privat"
                ["attendee_city"]       => string(4) "Test"
                ["attendee_sex"]        => string(3) "Man"
                ["attendee_dob"]        => string(10) "1980-01-15"
                ["attendee_email"]      => string(12) "test@test.nl"
                ["attendee_phone"]      => string(10) "0606060606"
                ["attendee_speed"]      => string(12) "testt - seln"
                ["attendee_shirtsize"]  => string(2) "XS"
            }
        }
    } 
    ["registration"]=> array(9) { 
        ["user_name"]   => string(5) "admin"
        ["user_email"]  => string(0) ""
        ["dbem_address"]=> string(4) "test"
        ["dbem_city"]   => string(4) "test"
        ["dbem_state"]  => string(4) "test"
        ["dbem_zip"]    => string(6) "8888ZZ"
        ["dbem_country"]=> string(2) "BD"
        ["dbem_phone"]  => string(0) ""
        ["dbem_fax"]    => string(0) "" 
    }
    ["booking"]=> array(1) {
        ["booking_comment"]=> string(0) "" 
    }
    ["gateway"]=> string(18) "idealcheckoutideal"
}

问题在于4可能是任何东西。 (但遗憾的是,我无法改变其运作方式) 有没有办法设置一个未指定的索引?

2 个答案:

答案 0 :(得分:3)

您可以使用array_values重置密钥:

array_values($data["attendees"])[0][0]["attendee_firstname"]

答案 1 :(得分:0)

您可以尝试使用基于0的索引:

$data["attendees"] = array_values($data["attendees"]);

或者这个迭代,无论键是什么:

foreach ($data["attendees"] as $idx => $value) {
    // do something with $value
}