2015-12-21 111 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