在解码的JSON中访问第二个数组(非对象)

时间:2020-01-08 22:56:13

标签: php arrays json multidimensional-array

我正在尝试访问此解码的json中的第二个数组(联系人),以获取first_name旁边的ID(两个ID键可能不同),但是第二个数组似乎不是对象,所以我找不到可以访问的循环:

stdClass Object (
    [data] => Array (
        [0] => stdClass Object (
            [account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
            [is_owner] => 1
            [id] => 1
            [name] => test test
            [display_name] => test test
            [balance] => 0
            [paid_to_date] => 0
            [updated_at] => 1578494555
            [archived_at] =>
            [address1] => Street
            [address2] =>
            [city] => Town
            [state] => State
            [postal_code] => Code
            [country_id] => 0
            [work_phone] => Number
            [private_notes] =>
            [public_notes] =>
            [last_login] =>
            [website] =>
            [industry_id] => 0
            [size_id] => 0
            [is_deleted] =>
            [payment_terms] => 30
            [vat_number] =>
            [id_number] =>
            [language_id] => 0
            [currency_id] => 0
            [custom_value1] =>
            [custom_value2] =>
            [invoice_number_counter] => 1
            [quote_number_counter] => 1
            [task_rate] => 0
            [shipping_address1] =>
            [shipping_address2] =>
            [shipping_city] =>
            [shipping_state] =>
            [shipping_postal_code] =>
            [shipping_country_id] => 0
            [show_tasks_in_portal] => 1
            [send_reminders] => 1
            [credit_number_counter] => 1
            [custom_messages] => {}
            [contacts] => Array (
                [0] => stdClass Object (
                    [account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv
                    [is_owner] => 1
                    [id] => 1
                    [first_name] => test
                    [last_name] => test
                    [email] => myemail@me.com
                    [contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1
                    [updated_at] => 1578494555
                    [archived_at] =>
                    [is_primary] => 1
                    [phone] => 07919446174
                    [last_login] =>
                    [send_invoice] => 1
                    [custom_value1] =>
                    [custom_value2] =>
                )
            )
        )
    )
    [meta] => stdClass Object (
        [pagination] => stdClass Object (
            [total] => 1
            [count] => 1
            [per_page] => 15
            [current_page] => 1
            [total_pages] => 1
            [links] => Array ( )
        )
    )
) 

这是我尝试过的,但是没有找到任何东西:

$person = getclient($itemid);
$person_data = json_decode($person);
foreach ($person_data->contacts as $key => $item)
{
$itemid = $item->id . "<br />";
}   

1 个答案:

答案 0 :(得分:0)

$person_data包含data属性,该属性是一个对象数组。

foreach ($person_data->data as $person) {
    foreach ($person->contacts as $contact) {
        echo $contact->id . '<br>';
    }
}