PHP多维数组抓取项

我有一个试图从中获取 ID 的 API,但输出是一个多维数组,我不能只获取 ID。


这是数组的样子:


Array ( [{ data] => [ { account_key [ is_owner] => true [ id] => 1 [ name] => test [ display_name] => test [ balance] => 0 [ paid_to_date] => 0 [ updated_at] => 1577724986 [ archived_at] => null [ address1] => [ address2] => [ city] => [ state] => [ postal_code] => [ country_id] => 0 [ work_phone] => [ private_notes] => [ public_notes] => [ last_login] => [ website] => [ industry_id] => 0 [ size_id] => 0 [ is_deleted] => false [ 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] => true [ send_reminders] => true [ credit_number_counter] => 1 [ custom_messages] => {} [ contacts] => [ { account_key [ is_owner] => true [ id] => 1 [ first_name] => test [ last_name] => test [ email] => me@idontlikespam.com [ contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1 [ updated_at] => 1577724986 [ archived_at] => null [ is_primary] => true [ phone] => [ last_login] => [ send_invoice] => true [ custom_value1] => [ custom_value2] => } ] } ] [ meta] => { pagination [ count] => 1 [ per_page] => 15 [ current_page] => 1 [ total_pages] => 1 [ links] => [] } } } ) 

这是我到目前为止将其拆分出来的代码:


    $clients = str_replace('"', "", $clients);

$convert_to_array = explode(',', $clients);

for($i=0; $i < count($convert_to_array ); $i++){

$key_value = explode(':', $convert_to_array [$i]);

$end_array[$key_value [0]] = $key_value [1];

}

print_r($end_array);    

foreach ($end_array as $key => $item)

{

    echo "[" . $key . "] => " . $item . "<br />";

    if ($key = ' id')

    {

        echo $item;

    }

}

这只是我错过的最后一步,这可能很容易,但我仍然在圣诞节大脑中奔跑......


繁星淼淼
浏览 80回答 1
1回答

慕妹3242003

在Array您可以通过 访问值[index]。在stdObject您可以访问属性,->因为它是对象。所以stdClass 对象 ( [data] => Array ( [0] => stdClass 对象 ( [account_key]IsObject->(property data has value array)[index 0 in array and is object]->(property account_key)在您的数据上,您需要这个。foreach ($end_array->data as $key => $item){&nbsp; &nbsp; echo $item->id . "<br />";}或者如果您知道数据是否可以始终包含一个您可以使用此快捷方式访问的项目echo $end_array->data[0]->id;
打开App,查看更多内容
随时随地看视频慕课网APP