Connecting Tech Pros Worldwide Forums | Help | Site Map

array of array

Bob Bedford
Guest
 
Posts: n/a
#1: Sep 29 '05
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



Kim André Akerĝ
Guest
 
Posts: n/a
#2: Sep 29 '05

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)
Ewoud Dronkert
Guest
 
Posts: n/a
#3: Sep 29 '05

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
Bob Bedford
Guest
 
Posts: n/a
#4: Sep 29 '05

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

Kim André Akerĝ
Guest
 
Posts: n/a
#5: Sep 29 '05

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