Wednesday, 1 October 2014

add array to array without key in PHP

$foo = array(
  'tags[]' => array('one','two','three','four')
);
My array looks like this

Array
(
    [0] => one
    [1] =>  two
    [2] =>  three
    [3] =>  four
    [4] =>  five
    [5] =>  six
)
I've tried adding the array


Answer1:
$foo = array(
  'tags[]' => array($arr)
);
 How do I add the values from $arr to the tags[]?


 Your tags[] array is one level too deep. It should be:

$foo = array(
  'tags[]' => $arr
);
Note how $arr is not wrapped in array().

If you don't remove the array() then it looks like:

$foo = array(
  'tags[]' => array(
      array('one', 'two', 'three')
  )
);

0 comments:

Post a Comment