添加到多维数组php

时间:2011-06-08 14:11:42

标签: php arrays multidimensional-array

我有一个这样的数组:

$where = array(
 'product_id' => $product_id,
 'item_id' => $item_id
);

我想根据条件添加到这个数组,所以我可以做

if($condition){
 $where = array()
}else{
 $where = array()
}

重复原始内容两次,但理想情况下,我想像array_push(array('id' => $id), $where);

那样做

谢谢!

2 个答案:

答案 0 :(得分:3)

您可以通过以下方式向阵列添加内容:

$where['mykey'] ='myvalue';

答案 1 :(得分:1)

只需通过指定索引和值将其添加到数组中。

if($condition){
 $where['id'] = $id;
}else{
 $where['other'] = $other;
}