Connecting Tech Pros Worldwide Help | Site Map

array of array

  #1  
Old September 29th, 2005, 10:05 AM
Bob Bedford
Guest
 
Posts: n/a
I've to save in an array of array some values.

Problem: I don't know the size of the array before filling it.

I'd like something like

$bigarray = array():

then runnig the code I must create dinamically (I don't know how many) few
arrays in the bigarray;
$bigarray["X"] = array():
$bigarray["Y"] = array():

then fill those arrays with few unknow number of values.
array_push($bigarray["X"],$value1);
array_push($bigarray["X"],$value2);

array_push($bigarray["Y"],$value5);
....
but array_push doesn't manage it.

How to do so ?

Bob


  #2  
Old September 29th, 2005, 10:15 AM
Kim André Akerĝ
Guest
 
Posts: n/a

re: array of array


Bob Bedford wrote:
[color=blue]
> I've to save in an array of array some values.
>
> Problem: I don't know the size of the array before filling it.
>
> I'd like something like
>
> $bigarray = array():
>
> then runnig the code I must create dinamically (I don't know how
> many) few arrays in the bigarray; $bigarray["X"] = array():
> $bigarray["Y"] = array():
>
> then fill those arrays with few unknow number of values.
> array_push($bigarray["X"],$value1);
> array_push($bigarray["X"],$value2);
>
> array_push($bigarray["Y"],$value5);
> ...
> but array_push doesn't manage it.
>
> How to do so ?[/color]

$bigarray["X"][] = $value1;
$bigarray["X"][] = $value2;
....
$bigarray["Y"][] = $value5;

--
Kim André Akerĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
  #3  
Old September 29th, 2005, 10:15 AM
Ewoud Dronkert
Guest
 
Posts: n/a

re: array of array


Bob Bedford wrote:[color=blue]
> array_push($bigarray["Y"],$value5);
> but array_push doesn't manage it.[/color]

Try $big_array['Y'][] = $value5;
In the manual it says "has the same effect" but maybe the implementation
is different? I don't know. It also says:

"Note: If you use array_push() to add one element to the array it's better
to use $array[] = because in that way there is no overhead of calling a
function."

--
E. Dronkert
  #4  
Old September 29th, 2005, 10:25 AM
Bob Bedford
Guest
 
Posts: n/a

re: array of array


> $bigarray["X"][] = $value1;[color=blue]
> $bigarray["X"][] = $value2;
> ...
> $bigarray["Y"][] = $value5;[/color]
Does it create a new element in the $bigarray["X"] ?
I mean then I have
$bigarray["X"][0] == $value1;
$bigarray["X"][1] == $value2;

Bob

  #5  
Old September 29th, 2005, 10:35 AM
Kim André Akerĝ
Guest
 
Posts: n/a

re: array of array


Bob Bedford wrote:
[color=blue][color=green]
> > $bigarray["X"][] = $value1;
> > $bigarray["X"][] = $value2;
> > ...
> > $bigarray["Y"][] = $value5;[/color]
> Does it create a new element in the $bigarray["X"] ?
> I mean then I have $bigarray["X"][0] == $value1;
> $bigarray["X"][1] == $value2;[/color]

Yes. As mentioned in Ewoud Dronkert's reply, this is a better way of
doing it rather than using array_push(). Even the PHP manual mentions
this.

$bigarray[] = $value1;
=>
$bigarray[0] == $value1;

$bigarray[0][] = $value2;
=>
$bigarray[0][0] == $value2;

--
Kim André Akerĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templated array of templates specialized by array index npankey@gmail.com answers 6 October 12th, 2008 04:25 PM
Sort array of struct based on the data members. Santosh Nayak answers 4 March 14th, 2007 06:55 AM
help generating an array of array with malloc or calloc Eric Boutin answers 7 November 13th, 2005 11:38 PM
Reference to array, NOT array of references! JKop answers 12 July 22nd, 2005 05:44 PM
array of...array.... Bob Bedford answers 8 July 17th, 2005 07:48 AM