duandie5707 2016-07-11 12:07
浏览 49

展平多维数组无法正常工作

I have two arrays like the following.

$alerts_array=array(1) {
          [0]=> array(11) {
                           ["CustomAlertsID"]=> int(3) 
                           ["CustomAlerts_Name"]=> string(10) "title demo" 
                           ["CustomAlerts_PublishDate"]=> string(10) "2016-07-03" 
                           ["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21"
                          }
            }
  $singlebtn_array  =array(3) {
       ["button_text0"]=> string(16) "Button Name1only"
       ["button_text1"]=> string(12) "button name2" 
       ["button_text2"]=> string(16) "button name3_new" 
     } 

I have merged the two arrays into a single multidimensional array which looks like following

$alerts_array = array_merge($alerts_array,$singlebtn_array);

array(4) { [0]=> array(11) 
               { ["CustomAlertsID"]=> int(3)
                 ["CustomAlerts_Name"]=> string(10) "title demo"   
                 ["CustomAlerts_PublishDate"]=> string(10) "2016-07-03" 
                 ["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21" 
               } 

           [1]=> array(1) 
                 { ["button_text0"]=> string(16) "Button Name1only" }
           [2]=> array(1) 
                 { ["button_text1"]=> string(12) "button name2" } 
           [3]=> array(1) { ["button_text2"]=> string(16) "button name3_new" } }

I need both keys and values in the new flattened array

I need it like this:

array(4) { [0]=> array(11) 
  { ["CustomAlertsID"]=> int(3)
    ["CustomAlerts_Name"]=> string(10) "title demo"   
    ["CustomAlerts_PublishDate"]=> string(10) "2016-07-03" 
    ["CustomAlerts_ExpiryDate"]=> string(10) "2016-07-21"  
    ["button_text0"]=> string(16) "Button Name1only"
    ["button_text1"]=> string(12) "button name2" 
    ["button_text2"]=> string(16) "button name3_new" }}

I have usedthe following code for combining.

 $newArr = array();
 foreach ($alerts_array as $key=>$tmp) {
    $newArr = array_merge($newArr, array_values($tmp));
 }

The $newArr is giving me the result ,but keys are lost

  • 写回答

2条回答 默认 最新

  • dsmupo6631 2016-07-11 12:18
    关注

    Simply merge the first key [0] of your $alerts_array, like this:

    $alerts_array = array_merge($alerts_array[0], $singlebtn_array);
    

    That will output:

    array(7) {
      ["CustomAlertsID"]=>
      int(3)
      ["CustomAlerts_Name"]=>
      string(10) "title demo"
      ["CustomAlerts_PublishDate"]=>
      string(10) "2016-07-03"
      ["CustomAlerts_ExpiryDate"]=>
      string(10) "2016-07-21"
      ["button_text0"]=>
      string(16) "Button Name1only"
      ["button_text1"]=>
      string(12) "button name2"
      ["button_text2"]=>
      string(16) "button name3_new"
    }
    

    Also see the working demo here.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教