Array_push giving unexpected (by me) result.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Array_push giving unexpected (by me) result.

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Array_push giving unexpected (by me) result.

Post 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;
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Array_push giving unexpected (by me) result.

Post by pizzipie »

SOLVED:

Thanks requinix,

Too simple!

R
Post Reply