将更多键和值放入现有数组中

将更多键和值放入现有数组中

问题描述:

I have an array like so:

$json = array('error'=>true);

But I'd like to perhaps add more keys and values to this at a later time. My feeble knowledge tried this:

$json .= array('something'=>'else');

Which doesn't work. I found array_push but it seems this is for just pushing in new values - not keys. How is this achieved so that with 2 separate declarations I end up with the equivalent of:

$json .= array('error'=>true,'something'=>'else');

我有一个这样的数组: p>

  $ json =  array('error'=> true); 
  code>  pre> 
 
 

但是我想在以后添加更多的键和值。 我的微弱知识尝试了这个: p>

  $ json。= array('something'=>'else'); 
  code>  pre> 
 \  n 

哪个不起作用。 我找到了 array_push code>,但似乎只是推送新值 - 而不是键。 如何实现这一点,以便使用2个单独的声明,最终得到相当于: p>

  $ json。= array('error'=> true,'something'=  >'else'); 
  code>  pre> 
  div>

there are many ways to accomplish this:

  1. $json['keyname'] = 'something

  2. $json[] = 'something' <- numerical incremented key

  3. array_push($json, 'value') <- same as above

  4. $json = array_merge($json, $some_other_array) <- mixes the two arrays together

Just keep in mind that arrays are not strings