duanjian9148 2014-08-29 20:31
浏览 58
已采纳

如何从多个数组生成单个数组

I have used array_merge(); but for some reson is not working the way I need it... so I hve multiple arrays with almost the same information as follow...

$array1 = array (
           '2014-06-01'=> array (
                          'date'=> '2014-06-01',
                          't1'=> 20,
                          't2'=> 5
                         );
           '2014-06-02'=> array (
                          'date'=> '2014-06-02',
                          't1'=> 20,
                         );
           '2014-06-03'=> array (
                          'date'=> '2014-06-03',
                          't1'=> 9,
                          't2'=> 2
                         );
         );
$array2 = array (
           '2014-07-01'=> array (
                          'date'=> '2014-07-01',
                          't1'=> 4,
                          't2'=> 1
                         );
           '2014-07-02'=> array (
                          'date'=> '2014-07-02',
                          't1'=> 6,
                          't2'=> 3
                         );
           '2014-07-03'=> array (
                          'date'=> '2014-07-03',
                          't1'=> 9,
                         );
         );

So that's how I have my arrays for one purpose, but instead of building another query to get another arrays with almost the same information I'd like to know how can I build another array from those 2 or 3 similar arrays...

the final array that I'd like to get is something like this...

$final_arr = array (
         '01' => array (
                 't2_m_jun'=>5,
                 't2_m_jul'=>1,
          );
         '02' => array (
                 't2_m_jun'=>0,
                 't2_m_jul'=>3,
          );
         '03' => array (
                 't2_m_jun'=>2,
                 't2_m_jul'=>0,
          );
   );

now, the 01, 02, 03...etc is the date from 1 to 30 or 31 depending on the month, the so from each array I need the date, if the index is 2014-06-01 I only need 01 and from that I need the value of t2, and for the second array is the same so that I can put'em together with new keys and values from t2 ...

I'm not good building functions or working with arrays that is why I need your help.
Thank you for taking the time to read my post.

  • 写回答

1条回答 默认 最新

  • dongwuxie5112 2014-08-29 20:51
    关注

    Well, first you need to put them together with array_merge and then loop through it and build the new array. This should do the trick:

    $new_array = array_merge($array1, $array2);
    $final_arr = array();
    foreach($new_array as $val) {
        $timestamp = strtotime($val['date']);
        $day = date('d', $timestamp);
        if(empty($final_arr[$day])) {
            $final_arr[$day] = array();
        }
    
        $month = strtolower(date('M', $timestamp));
        $final_arr[$day]['t2_m_'.$month] = $val['t2'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据