dsyk33753
2016-05-24 12:18
浏览 44
已采纳

如何在PHP中附加到另一个数组中的数组

I have an array that contains 4 elements (sub-arrays).

$orders = array( array(), array(), array(), array() );

Now, I want to tell each sub-array to append items to itself, so I iterate through them like so:

for($i=0; $i<4 ; $i++ ) {
  $orders[$i][] = rand();     // this does not work, unexpected '['
}

What is a better way for accomplishing this?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmeng2687 2016-05-24 12:21
    已采纳

    You had a syntax error:

    for($i=0; $i<4 ; $i++ ) {
       $orders[$i][] = rand();     // this does not work, unexpected '['
    }
    

    But this might be even better:

    foreach($orders as $key => $order){
        $orders[$key][] = rand();
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题