Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

Why wont array_push add item to my array?

Why is this not adding the new idnum of 2 to the end of the array?

$idnum = 2;
$array01 = Input::get('user_ids');
$array02 = array_push($array01, $idnum);

$finalarray = array_keys($array02);
0 likes
8 replies
jpeterson579's avatar

@pmall Thanks for info but when I do this

$idnum = 2;
$array01 = Input::get('pool_ids');
array_push($array01, $idnum);
$finalarray = array_keys($array01);

Its not adding the 2 to my $array01. Instead its adding the next increemental value. So for example if I have 112, 113, 114 in my array, its outputting 112, 113, 114, 115.
Not 112, 113, 114, 2 like i want

pmall's avatar

Of course you cant see 2 at the end of the array, you are looking for the keys. Next key after 114 is 115.

anouarabsslm's avatar

why don't you turn your array to collection like so:

$idnum = 2;
$array01 = new  Illuminate\Support\Collection(Input::get('pool_ids'));
$array01->push(2);
dd($array01);
pmall's avatar

$array01[$idnum] = 'value';

Man I think you need some sleep :)

Please or to participate in this conversation.