-1

PHP - I am trying to push items into an array by using array_push.But it returns number of items in it.Not an array

$items = array();

$item1 = 'hello';
$item2 = 'hola';

$items = array_push($items, $item1, $item2);
print_r($items);

How can I get The $item as an array?

Tick Twitch
  • 513
  • 10
  • 23
  • Possible duplicate of [Difference between array\_push() and $array\[\] =](https://stackoverflow.com/questions/14232766/difference-between-array-push-and-array) – mickmackusa Jun 26 '18 at 02:52
  • This is [a link to the manual](http://php.net/manual/en/function.array-push.php) <-- the first place that you should look. – mickmackusa Jun 26 '18 at 02:54

1 Answers1

0

array_push() returns the number of items in the array so don't assign array_push to $items and it should work as intended

$items = array();

$item1 = 'hello';
$item2 = 'hola';

array_push($items, $item1, $item2); // removed "$items =" from this line 
print_r($items);

Essentially your example was adding the elements to $items then overwriting it with count($items) due to array_push's return value

Brian
  • 2,822
  • 1
  • 16
  • 19