dongyue4964 2014-10-11 13:07
浏览 44
已采纳

从多维数组中提取和分组数据

I want to print out information from a multidimensional array.

Here is my array structure:

// A two-dimensional array
$cars=array
(
    array('service' => "Windows8",'host' => Sydney1,'state' => on),
    array('service' => "unix",'host' => Newyork2,'state' => off),
    array('service' => "Windows8",'host' => Singapore3,'state' => on),
    array('service' => "unix",'host' => Tokyo4,'state' => off),
    array('service' => "Windows8",'host' => Tokyo4,'state' => on),
);

I want to arrange the data to print out the following:

Data grouped by service: desired output:

Windows8 (3): Sydney1 (on), Singapore3 (on), Tokyo4 (on) 
unix (2): Newyork2 (off), Tokyo4 (off)

Data grouped by host: desired output:

Sydney1 (1): Windows8 (on)
Newyork2 (1): unix (off)
Singapore3 (1): Windows8 (on)
Tokyo4 (2): unix (off), Windows8 (on)

Here is the code I have tried so far:

    $arr = array();

    foreach($cars as $key => $item)
    {
       $arr[$item['service_name']][$key] = $item;
    }
    echo '<pre>';    
    print_r($arr);
    echo '</pre>';

    $size = sizeof($arr);
    $i=1;
    foreach($arr as $key => $item)
    {
        echo $key;
    }
  • 写回答

3条回答 默认 最新

  • doufangyan6862 2014-10-11 14:08
    关注

    The trick to manipulating arrays is to arranging them into data structures that will make it easy for you to retrieve the data you need.

    $cars=array (
           array('service' => "Windows8",'host' => 'Sydney1','state' => 'on'),
           array('service' => "unix",'host' => 'Newyork2','state' => 'off'),
           array('service' => "Windows8",'host' => 'Singapore3','state' => 'on'),
           array('service' => "unix",'host' => 'Tokyo4','state' => 'off'),
           array('service' => "Windows8",'host' => 'Tokyo4','state' => 'on'),
    );
    
    # initialise an array to group data by service and by location
    $data = array();
    
    foreach ($cars as $c) {
        # the key is the service name, and the value is an array of host/state pairs
        $data['by_service'][ $c['service'] ][] = array( $c['host'] , $c['state'] );
    
        # the key is the location name, and the value is an array of service/state pairs
        $data['by_location'][ $c['host'] ][] = array( $c['service'] , $c['state'] );
    }
    
    # create a function that we can use to print data from the pairs in the
    # data structures above: it creates a string with the first array item
    # followed by the second item in brackets
    function print_arr ($arr) {
        return $arr[0] . " (" . $arr[1] . ")";
    }
    
    # group by service:
    foreach ($data['by_service'] as $s => $host) {
        # print the service type, $s, and count the number of hosts
        echo "$s (" . count($host) . "): ";
    
        # print the details for the individual hosts, using print_arr for the details
        # array_map is an extremely useful function that allows you to apply a function
        # to every member of an array. It saves having to create new arrays or alter
        # the array using foreach and you can include it in an echo statement.
        echo implode(", ", array_map( "print_arr", $host ) ) . "
    ";
    }
    
    echo "
    
    ";
    
    # group by location
    foreach ($data['by_location'] as $l => $host) {
        # print the location and the count for the number of hosts
        echo "$l (" . count($host) . "): ";
        echo implode(", ", array_map( "print_arr", $host ) ) . "
    ";
    }
    

    Output:

    Windows8 (3): Sydney1 (on), Singapore3 (on), Tokyo4 (on)
    unix (2): Newyork2 (off), Tokyo4 (off)
    
    
    Sydney1 (1): Windows8 (on)
    Newyork2 (1): unix (off)
    Singapore3 (1): Windows8 (on)
    Tokyo4 (2): unix (off), Windows8 (on)
    

    You might find a print_r of the $data structure helpful if you're getting lost in the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集