dtr32221 2013-07-18 16:04
浏览 33
已采纳

从多个阵列创建主阵列

I have 2 arrays(which could be more too) and they might have scattering data.

Date    | Apple                       Date    | Banana  
-----------------                    -------------------
   1      |   5                         1     |   3
   4      |   5                         3     |   1
   5      |   5                         7     |   2

The array structure would be like as below.

array
(
   [0] => array
   (
      [date] => 4
      [apple] => 5
   )
   [1] => array
   (
       [date] => 5
       [apple] => 5
   )
)

What I need is to have a consolidated array having both the details. Example shown below.

Date      Apple      Banana
-------------------------------    
 1          5          3
 3          0          1
 4          5          0
 5          5          0
 7          0          2

How best we can do this considering the fact that array can hold multiple values? Any hint will help me to solve this. Or please point me to correct link if its already discussed. I could not find anything during search.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • douduikai0562 2013-07-18 18:14
    关注

    Based on @Hristo response, I modified the code and it worked the following way. By this way, I get the expected output.

    Below is not the exact code that I used. I have modified to be in alignment with my example posted in the question.

    Will this work well in a huge array?

        $finalArray = array();
    
        foreach ($apple as $key => $value) {
             $finalArray[$value['date']]['apple'] = $value['apple'];
        }
    
        foreach ($banana as $key => $value) {
             $finalArray[$value['date']]['banana'] = $value['banana'];
        }
    
        var_dump($finalArray);
    

    This gives an output of

     array
     (
         [1] => array
         (
            [apple] => 5
            [banana] => 3
         )
     )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大