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



嗨,我有一个多维数组,我使用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
             )
    )
)

我试着让我的输出像这样,试着格式化它,这样我就可以用它在highcharts中用于下拉系列,谢谢你

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

首先,您不能在数组中使用相同键的字段两次,例如salescountsalestat。但是你可以这样做。

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;
}

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

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

你可以这样使用:

$employees = wrapSameId( $PDOArray );

其中$PDOArray是您的初始数组。

为什么不按id索引输出数组呢?

循环遍历初始数组,并将salecountsalestat对添加到员工的数据数组。

$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);
}

这正是你想要的

相关内容

  • 没有找到相关文章

最新更新