douduan5086 2017-02-28 15:00
浏览 58
已采纳

将数组合并到多维数组中

i have dynamic number of arrays and i need to make them a multidimensional array

Array ( [id] => 182 [name] => item1 ) 
Array ( [id] => 185 [name] => item2 ) 
Array ( [id] => 186 [name] => item3 ) 

to this

$all = Array ( Array ( [0] => Array ( [id] => 182 [name] => item1 ) , 
               Array ( [1] => Array ( [id] => 185 [name] => item2 ) ,
               Array ( [2] => Array ( [id] => 186 [name] => item3 ) 
             )

i tries this code but it's not working

$all_ids = Array ( [0] => 182 [1] => 185 [2] => 186 );
foreach($all_id as $id){
    // use code to return row
    $row= 'select id , name from table where id ='.$id; // something like this which return row
    $all= $all+ $row ;
}

any help please , thanks in advance

  • 写回答

2条回答 默认 最新

  • dongshi6529 2017-02-28 15:04
    关注

    While I don't exactly understand what is to be achieved, you could declare $all as an array outside of the loop

    $all = array();
    

    and inside the loop, instead of

    $all= $all+ $row ;
    

    do

    $all[] = $row;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址