2015-12-21 49 views
0

我使用CURL處理輸出多維數組的多個請求。我需要從輸出中提取3個值(url,content_type和http_code),然後分別將每個值分配給一個新數組,即$array1=url $array2=content_type $array3=http_code.從多維數組中提取數值php

我嘗試使用for語句來提取每個值,噸按預期方式工作,我得到

「未定義偏移:」

陣列輸出

陣列([http://example1/] =>陣列([URL] =>http://example1/ [內容] => [HTTP_CODE ] => 40 5 [header_size] => 249 [request_size] => 48 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.328 [namelookup_time] => 0 [connect_time] => 0.172 [pretransfer_time] => 0.172 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [upload_content_length] => -1 [ start_transfer_time] => 0.328 [redirect_time] => 0 [redirect_url] => [primary_ip] => 204.79.197.200 [certinfo] => Array()[primary_port] => 80 [local_ip] => 192.168.2.12 [local_port ] => 55536)[http://example2/] => Array([url] =>http://example2/ [content_type] => text/html; charset = utf-8 [http_code] => 200 [header_size] => 699 [request_size] => 57 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.453 [ namelookup_time] => 0.015 [connect_time] => 0.203 [pretransfer_time] => 0.203 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 1 [upload_content_length] => -1 [starttransfer_time] => 0.453 [redirect_time] => 0 [redirect_url] => [primary_ip] => 104.16.34.249 [certinfo] => Array()[primary_port] => 80 [local_ip] => 192.168.2.12 [local_port] => 55539))

for($y=0;$y<= (count($array1)-1);$y++){ 
     echo $array1 [$y][0][1][2]; 
    } 
+0

您能否正確地格式化代碼?另外,這是什麼樣的輸出(逗號缺失)? – Jan

+0

這不是我的代碼,它是print_r的輸出。我有一個函數使用多個捲髮來輸出上面的結果。 – user2334436

+1

小改進 –

回答

0

如果你想獲得這樣的:

$array1 = [ 
    'content_type' => ['', 'text/html; charset=utf-8'] 
]; 

$array2 = [ 
    'url' => ['http://example1/', 'http://example2/'] 
]; 

$array3 = [ 
    'http_code' => [405, 200] 
]; 

然後嘗試使用foreach循環。

foreach ($output as $url => $values) { 
    $array1['content_type'][] = $values['content_type']; 
    $array2['url'][] = $values['url']; 
    $array3['http_code'][] = $values['http_code']; 
} 

查看更多about foreach loopArray type。同樣在關聯數組中,您不能使用數字索引獲取值,而是使用字符串而不是。

+0

工作!謝謝。我從來沒有使用過多維/關聯數組,所以這是一個挑戰。我需要更多地瞭解它。 – user2334436