简体   繁体   中英

Use unspecified index array php

I've got an associative array with (it seems like) a dynamic integer. The array:

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"
}

For example, if I want to get the attendee_firstname I use the following:

$data["attendees"][4][0]["attendee_firstname"]

The problem is that the 4 could be anything. (But I can't change the way this works either, sadly) Is there a way to set an unspecified index?

您可以使用array_values重置键:

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

You can try this to have a 0-based index:

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

or this to iterate, no matter the keys:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM