dougan4884 2015-08-07 06:14
浏览 57
已采纳

如何在一个多维数组中组合内部数组值?

Hi I have one multidimensional array, I retrieved the data using mysql PDO and formatted it this way

array('id'=>$row['empname'],'data'=>array(array($row['salestat'],$row['salecount'])))

Array
(
[0] => Array
    (           
        [id] => 'employee1'
        [data] => Array
             (
              0 => 'Sold'
              1 => 1
             )
    )

[1] => Array
    (            
        [id] => 'employee2'
        [data] => Array
             (
              0 => 'On Process'
              1 => 4
             )
    )

[2] => Array
    (            
        [id] => 'employee2'
        [data] => Array
             (
              0 => 'Sold'
              1 => 2
             )
    )
)

I am trying to make my output like this, trying to format this so I can use it for drilldown series in highcharts, thank you

Array
(
[0] => Array
    (           
        [id] => 'employee1'
        [data] => Array
             (
              0 => 'Sold'
              1 => 1
             )
    )

[1] => Array
    (            
        [id] => 'employee2'
        [data] => Array
           [0]
             (
              0 => 'On Process'
              1 => 4
             )
           [1]
             (
              0 => 'Sold'
              1 => 2
             )
    )
)
  • 写回答

2条回答 默认 最新

  • dpca31461 2015-08-07 06:47
    关注

    Firstly, you can not use a field inside an array twice with the same key like salescount or salestat. But you can do something like this.

    function wrapSameId( $employeeArray ) {
    
        //create the new array.
        $newEmployeeArray = array();
    
        //loop the old array.
        foreach ($employeeArray as $key => $value) {
            $exists = 0;
            $i=0;
    
            $id = $value['id'];
            $data = $value['data'];
    
            //see if you can find the current id inside the newly created array if you do, only push the new data in.
            foreach ($newEmployeeArray as $key2 => $value2) {
                if( $id == $value2['id'] ) { $newEmployeeArray[$key2]['data'][] = $data;$exists = 1;var_dump($value2['data']); }
                $i++;
            }
    
            //if we didnt find the id inside the newly created array, we push the id and the new data inside it now.
            if( $exists == 0 ){
                $newEmployeeArray[$i]['id'] = $id;
                $newEmployeeArray[$i]['data'][] = $data; 
            }
        }   
    
        return $newEmployeeArray;
    
    }
    

    This function should return a new Array but if in the old array there were multiple arrays with the same id, in this one we should have:

    Array
    (
        [0] => Array
            (
                [id] => employee1
                [data] => Array
                    (
                        [0] => Array
                            (
                                [salescount] => 4
                                [salesstat] => Sold
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [id] => employee2
                [data] => Array
                    (
                        [0] => Array
                            (
                                [salescount] => 2
                                [salesstat] => In Progress
                            )
    
                        [1] => Array
                            (
                                [salescount] => 5
                                [salesstat] => Sold
                            )
    
                    )
    
            )
    
    )
    

    You can use it like this:

    $employees = wrapSameId( $PDOArray );
    

    where $PDOArray is your initial Array. Hope this is what you were looking for.

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

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要