根据条件将值从一个关联数组添加到另一个关联数组

时间:2013-06-23 12:41:59

标签: php arrays format traversal associative

到目前为止,我有三个数组从数据库中返回值(经过多次格式化)

[0]=>array(
           [0]=>'Apr', [1]=> 1), 
[1]=>array(
           [0]=>'May', [1]=>2), 
[2]=>array(
           [0]=>'Jun', [1]=>23)

具有相似值的第二个数组:

[0]=>array(
           [0]=>'Mar', [1]=>22),     
[1]=>array(
           [0]=>'Apr', [1]=> 1), 
[2]=>array(
           [0]=>'May', [1]=>2), 
[3]=>array(
           [0]=>'Jun', [1]=>25)

第三个:

[0]=>array(
           [0]=>'Jan', [1]=>50),     
[1]=>array(
           [0]=>'Feb', [1]=> 20), 
[2]=>array(
           [0]=>'Mar', [1]=>16), 
[3]=>array(
           [0]=>'Jun', [1]=>5)

这就是我试图让它们成为:

[0]=>array(
           [0]=>'Month', [1]=>'Calc1', [2]=>'Calc2', [3]=>'Calc3'),     
[1]=>array(
           [0]=>'Jan', [1]=>0, [2]=>0, [3]=>50), 
[2]=>array(
           [0]=>'Feb', [1]=>0, [2]=>0, [3]=>20), 
[3]=>array(
           [0]=>'Mar', [1]=>0, [2]=>22, [3]=>16),
[4]=>array(
           [0]=>'Apr', [1]=>1, [2]=>1, [3]=>0),
[5]=>array(
           [0]=>'May', [1]=>2, [2]=>2, [3]=>0),
[6]=>array(
           [0]=>'Jun', [1]=>23, [2]=>25, [3]=>5)

请注意如何在没有值的地方填写“0”。老实说,这是我遇到的最复杂的阵列问题,原因是难以遍历阵列。我特别需要将数组作为此结构,因为我需要将其发送到Google Visualization Area Chart。

如果您对此问题有任何建议,请发帖。

提前非常感谢你:)

@ DevZer0: 这是我得到的输出(使用我的真实数据)。请注意几个月的重复情况:

   array(
        [0] =>
            array(
                  [0] =>'Month'
                  [1] =>'Calc1'
                  [2] =>'Calc2'
                  [3] =>'Calc3'
                 )
        [1] =>
            array(

                  [0] =>'Apr'
                  [1] =>1
                  [2] =>0
                  [3] =>0
                  )
        [2] =>
            array(

                  [0] =>'Jun'
                  [1] =>9
                  [2] =>0
                  [3] =>0
                  )
        [3] =>
            array(

                  [0] =>'Apr'
                  [1] =>0
                  [2] =>1
                  [3] =>0
                  )
        [4] =>
                  array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>2
                  [3] =>0
                  )
        [5] =>
                  array(

                  [0] =>'Jun'
                  [1] =>0
                  [2] =>23
                  [3] =>0
                  )
        [6] =>
                  array(

                  [0] =>'Apr'
                  [1] =>0
                  [2] =>0
                  [3] =>1
                  )
        [7] =>
                  array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>0
                  [3] =>3
                  )
        [8] =>
                  array(

                  [0] =>'Jun'
                  [1] =>0
                  [2] =>0
                  [3] =>27
                  )
)

我希望他们以这种格式连接起来:

    array(
        [0] =>
            array(
                  [0] =>'Month'
                  [1] =>'Calc1'
                  [2] =>'Calc2'
                  [3] =>'Calc3'
                 )
        [1] =>
            array(

                  [0] =>'Apr'
                  [1] =>1
                  [2] =>1
                  [3] =>1
                  )
        [2] =>
            array(

                  [0] =>'May'
                  [1] =>0
                  [2] =>2
                  [3] =>3
                  )

        [3] =>
            array(

                  [0] =>'Jun'
                  [1] =>9
                  [2] =>23
                  [3] =>27
                  )

)

有办法吗?再次感谢你:))

1 个答案:

答案 0 :(得分:3)

这不是一个难题,如果我理解正确你有数月有一个整数。假设您有3个名为$array1$array2$array3

的数组
 $arrays = array($array1, $array2, $array3);
 $final = array();
 $index = 1;
 foreach ($arrays as $array) {
     foreach ($array as $ar) {
          if (!isset($final[$ar[0]])) {
              $final[$ar[0]] = array($ar[0], $index => $ar[1]);
          } else {
              $final[$ar[0]][$index] = $ar[1];
          }
     }
     $index++;
 }

 $final = array_values($final);

 for($x=0; $x < count($final); $x++) {
      for($i=1; $i < 4; $i++) {
          if (!isset($final[$x][$i])) $final[$x][$i] = "0";
      }
 }

 $header = array("Month", "Calc1", "Calc2", "Calc3");
 array_unshift($final, $header);

 var_dump($final); //will have your resulting array