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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?