[PHP] Arrays within arrays question

2002-08-28 Thread Jean-Christian Imbeault

I have the following loop to insert data into an array:

while ($data = pg_fetch_object($res)) {
 $aProds[] = array('id' = $data-prod_id, 'quantity' = 
$data-quantity);
   }

But when I print this out using print_r I get the following:

Array
(
 [0] = Array
 (
 [0] = Array
 (
 [id] = 289000100024
 [quantity] = 1
 )

 [1] = Array
 (
 [id] = 289000100050
 [quantity] = 1
 )

 )

)

This array has only one element in it; not what I wanted. I wanted 
something like this:

Array
(
 [0] = Array
 (
 [id] = 289000100024
 [quantity] = 1
 )
 [1] = Array
 (
 [id] = 289000100050
 [quantity] = 1
 )
)

How can I achieve this?

Thanks!

Jc


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within arrays question

2002-08-28 Thread Justin French

Okay, it may be the end of a long day here, but I can't tell the difference
between the two arrays you posted!

Justin



on 28/08/02 5:31 PM, Jean-Christian Imbeault ([EMAIL PROTECTED])
wrote:

 I have the following loop to insert data into an array:
 
 while ($data = pg_fetch_object($res)) {
 $aProds[] = array('id' = $data-prod_id, 'quantity' =
 $data-quantity);
 }
 
 But when I print this out using print_r I get the following:
 
 Array
 (
 [0] = Array
 (
 [0] = Array
 (
 [id] = 289000100024
 [quantity] = 1
 )
 
 [1] = Array
 (
 [id] = 289000100050
 [quantity] = 1
 )
 
 )
 
 )
 
 This array has only one element in it; not what I wanted. I wanted
 something like this:
 
 Array
 (
 [0] = Array
 (
 [id] = 289000100024
 [quantity] = 1
 )
 [1] = Array
 (
 [id] = 289000100050
 [quantity] = 1
 )
 )
 
 How can I achieve this?
 
 Thanks!
 
 Jc
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within arrays question

2002-08-28 Thread Jean-Christian Imbeault

Justin French wrote:
 Okay, it may be the end of a long day here, but I can't tell the difference
 between the two arrays you posted!

One array contains only one element. That one element contains two 
elements, each and array with two elements.

The other array contains two elements, each element being an array 
itself with two elements.

Jc


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within arrays question

2002-08-28 Thread Jason Wong

On Wednesday 28 August 2002 15:31, Jean-Christian Imbeault wrote:
 I have the following loop to insert data into an array:

 while ($data = pg_fetch_object($res)) {
  $aProds[] = array('id' = $data-prod_id, 'quantity' =
 $data-quantity);
}

 But when I print this out using print_r I get the following:

 Array
 (
  [0] = Array
  (
  [0] = Array
  (
  [id] = 289000100024
  [quantity] = 1
  )

  [1] = Array
  (
  [id] = 289000100050
  [quantity] = 1
  )

  )

 )

 This array has only one element in it; not what I wanted. I wanted
 something like this:

 Array
 (
  [0] = Array
  (
  [id] = 289000100024
  [quantity] = 1
  )
  [1] = Array
  (
  [id] = 289000100050
  [quantity] = 1
  )
 )

 How can I achieve this?

If $aProds can contain more than 1 item then what you're doing now is correct. 

If $aProds will only ever contain 1 item then use:

  $aProds = array('id' = $data-prod_id, 'quantity' = $data-quantity);

But then you can drop the while-loop and probably can just do:
  
  $aProds = pg_fetch_object($res);

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
Q:  What is the difference between a duck?
A:  One leg is both the same.
*/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within arrays question

2002-08-28 Thread Jean-Christian Imbeault

Jason Wong wrote:
 
 If $aProds can contain more than 1 item then what you're doing now is correct. 

Thanks. The problem was that I was doing this:

$return[] = get_array_of_prods();

the [] was creating the extra array level ...

The problem I now face is that foreach keeps crapping out if I pass it 
an unitialized (i.e. has no value) array. Foreach is happy with empty 
arrays but not arrays that have not been assigned anything yet ...

Jc


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Arrays within arrays question

2002-08-28 Thread Brent Baisley

I always put an if(is_array($var)) to check if an array was actually 
return.

On Wednesday, August 28, 2002, at 04:49 AM, Jean-Christian Imbeault 
wrote:

 Jason Wong wrote:
 If $aProds can contain more than 1 item then what you're doing now is 
 correct.

 Thanks. The problem was that I was doing this:

 $return[] = get_array_of_prods();

 the [] was creating the extra array level ...

 The problem I now face is that foreach keeps crapping out if I pass it 
 an unitialized (i.e. has no value) array. Foreach is happy with empty 
 arrays but not arrays that have not been assigned anything yet ...

 Jc

 -- PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php