普通网友 2015-12-21 17:55
浏览 42
已采纳

从多维数组php中提取值

I'm using CURL with multiple requests which outputs multi-dimensional array. I need to extract 3 of the values from the output (url , content_type, and http_code ) then assign each to a new array separately i.e. $array1=url $array2=content_type $array3=http_code.

I tried to use a for statement to extract each of the values but that didn't work as expected, I got

" Undefined offset:"

Array output

Array ( [http://example1/] => Array ( [url] => http://example1/ [content_type] => [http_code] => 405 [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 [starttransfer_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];
  }
  • 写回答

1条回答 默认 最新

  • duanjianhe1388 2015-12-21 18:30
    关注

    If you want to get this:

    $array1 = [
        'content_type' => ['', 'text/html; charset=utf-8']
    ];
    
    $array2 = [
        'url' => ['http://example1/', 'http://example2/']
    ];
    
    $array3 = [
        'http_code' => [405, 200]
    ];
    

    Then try to use foreach loop.

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

    Read more about foreach loop and Array type. Also in associative arrays you can't get values using numeric indexes, there strings instead of.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决