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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?