Re: Odd array_push issue
| From: | Richard S. Crawford | Date: | Wed, 11 May 2011 20:39:56 +0000 |
| Subject: | Re: Odd array_push issue | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312772@lists.php.net to get a copy of this message | ||
On Wed, May 11, 2011 at 1:30 PM, Peter Lind <peter.e.lind@gmail.com> wrote:
> On 11 May 2011 22:23, Richard S. Crawford <richard@underpope.com> wrote:
> > I'm encountering what appears to be a bug in array_push when I try using
> > that function to add objects to an array. For example...
> >
> > A = Object 1
> > B = Object 2
> >
> > If I execute the following code:
> >
> > array_push(objectarray, A);
> > array_push(objectarray, B);
> >
> > ...I expect the contents of $objectarray to be:
> >
> > [0] = A
> > [1] = B
> >
> > Instead, the last object pushed onto the array is repeated throughout the
> > array. In other words, instead of the above, I get this:
> >
> > [0] = B
> > [1] = B
> >
> > Can anyone enlighten me as to why this is happening? I've looked through
> bug
> > reports but haven't found anything, which leads me to think that perhaps
> my
> > own code is at fault.
> >
>
> If you could post some more of your code, it would be easier to check
> if your code is at fault or not.
>
> Regards
> Peter
>
Here's some more sample code, then:
============================
function retrieve_records ($where) {
(Returns an array of objects which represent rows from a database
query)
}
$records = retrieve_records($condition);
$recordlist = array();
foreach ($records as $record) {
array_push ($recordlist, $record)
}
============================
I know this code isn't very useful, but these are the conditions which cause
the issue I've mentioned.
--
Sláinte,
Richard S. Crawford (richard@underpope.com)
http://www.underpope.com