多维数组合

时间:2014-08-13 10:42:54

标签: php arrays sorting

我有这种多维数组结构。

Array
(
[labels] => Array
    (
        [0] => Array
            (
                [0] => NR
                [1] => Project
                [2] => Worker
               )
        [1] => Array
            (
                [0] => NR
                [1] => Project
                [2] => Worker
                [3] => Created
            )
[values] => Array
     (
         [0] => Array
             (
                [0] => 001
                [1] => Project example 1
                [2] => Some worker
             )
         [1] => Array
            (
                [0] => 002
                [1] => Project example 2
                [2] => Some worker 2
                [3] => 13/08/2014
            )
)

我想要实现的是:

Array
(
[0] => Array
    (
        [NR] => 001
        [Project] => Project example 1
        [Worker] => Some worker
    )
[1] => Array
    (
        [NR] => 002
        [Project] => Project example 2
        [Worker] => Some worker 2
        [Created] => 13/08/2014
     )
)

一些想法?提前致谢。 我试过这个:

$mega_outside = array();
$k=0;
foreach ($outside_arr as $arr => $value){
if (is_array($value)){

$mega_outside []= array_combine($arr['labels'][$k], $arr['values'][$k]);
    $k++;
    }
}

但它似乎不起作用......

4 个答案:

答案 0 :(得分:2)

这应该有效

$result = array_map('array_combine', $data['labels'], $data['values'])

答案 1 :(得分:1)

尝试这样做

$result = array();
$size = min(count($arr['labels']), count($arr['values']));
for($i = 0; $i < $size; $i ++)
    $result[] = array_combine($arr['labels'][$i], $arr['values'][$i]);

答案 2 :(得分:0)

试试此代码

$mega_outside = array();
foreach($outside_arr["labels"] as $k => $v){
    $mega_outside[$k] = array();
    foreach($v as $k1=>$v1){
       $mega_outside[$k][$v1] = $outside_arr['values'][$k][$k1];
    }
}
print_r($mega_outside);

DEMO

答案 3 :(得分:0)

<?php
$array1 = 
[labels] => Array ( 
     [0] => Array ( 
          [0] => NR 
          [1] => Project 
          [2] => Worker 
     ) 
     [1] => Array ( 
          [0] => NR 
          [1] => Project 
          [2] => Worker 
          [3] => Created 
      ) ;
      print_r( array_merge( $array1 ) 
);
?>