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

时间:2015-08-07 06:14:06

标签: php arrays multidimensional-array pdo highcharts

您好我有一个多维数组,我使用mysql PDO检索数据并以这种方式格式化

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

我正在尝试制作这样的输出,尝试格式化,以便我可以将其用于高级图中的向下钻取系列,谢谢

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 个答案:

答案 0 :(得分:1)

首先,您不能使用PersonPerson之类的相同键两次使用数组内的字段。但你可以这样做。

<test name="My suite">
  <groups>
    <dependencies>
      <group name="c" depends-on="a  b" />
      <group name="z" depends-on="c" />
    </dependencies>
  </groups>
</test>

这个函数应该返回一个新的数组但是如果在旧数组中有多个具有相同id的数组,在这一个我们应该有:

salescount

你可以像这样使用它:

salestat

其中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; } 是您的初始数组。希望这是你想要的。

答案 1 :(得分:0)

为什么不通过 id 索引输出数组?

遍历您的初始阵列,并将 salecount salestat 对添加到员工的数据阵列中。

$outputArray = array();

//Loop over each entry pulled from you database...
foreach ($employeeArray as $employeeInfo)
{
    //If no index for the current employee exists, create an empty array
    //The only reason I'm including this is because I'm not sure how array_merge handles nulls -- just to be safe
    if (!$outputArray[$employeeInfo['id']])
    {
        $outputArray[$employeeInfo['id']] = array();
    }

    //Merge the existing array for that employee with the new data for the same employee
    $outputArray[$employeeInfo['id']] = array_merge($outputArray[$employeeInfo['id']], $employeeInfo['data']);
}

print_r($outputArray);

将输出可视化更容易......

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

[employee2] => Array
            (            
                 0 => 4
                 1 => 'On Process'
                 2 => 2
                 3 => 'Sold'
            )
)

如果您对通过ID索引的输出不满意,请再次循环并根据需要对其进行格式化。

$otherOutputArray = array();
foreach ($outputArray as $key => $value)
{
    $otherOutputArray[] = array('id' => $key, 'data' => $value);
}

这正是你想要的。