Page 1 of 1

Array_push giving unexpected (by me) result.

Posted: Wed Mar 18, 2015 9:12 pm
by pizzipie
I'm trying to add contents of $db to array $rows. $db contains "bridge".

The result of this is two additions; One: :bridge", two "45" (the new length of the array).
I don't want "45" added to the array. Documentation suggests only " bridge" should have been added.

How can I fix this?

Appreciate help;
R

Code: Select all

[b]$rows[]=array_push($rows, $db);[/b]
[43] => Array
(
[id] => 177
[playdate] => 2015-03-02
[player] => Rupp, Kathy
[score] => 2750
)

[44] => bridge
[45] => 45

Re: Array_push giving unexpected (by me) result.

Posted: Wed Mar 18, 2015 9:36 pm
by requinix
Thanks for checking the documentation first. You did miss something important though: the return value.
Return Values
Returns the new number of elements in the array.
You then took that return value and appended it to $rows.

It's either one or the other: array_push() or the [] operator. The former is simpler.

Code: Select all

$rows[] = $db;

Re: Array_push giving unexpected (by me) result.

Posted: Wed Mar 18, 2015 10:14 pm
by pizzipie
SOLVED:

Thanks requinix,

Too simple!

R