Page 1 of 1

Problem using multidimensional array whithout initialisation

Posted: Wed Feb 17, 2010 5:24 pm
by mebibou
Hi everyone,

I'm running into some problems when using multidimensional arrays. What I want is an array containing multiple arrays, but using only two arrays: one multidimensional, the other one unidimensional.
Here's how it works:

- first I initialize the arrays
$multi_array = array(array());
$uni_array = array();

- then I need to retrieve some info on the web, that I want to store in an array
$uni_array[] = ("1");
$uni_array[] = ("blabla");
$uni_array[] = ("blablabla");

- then I want to store it in the multidimensional array, to be able to use it after
// I know that I need to copy the $uni_array and think the problem is here (tried to do it but never get it worked)
$multi_array[] = $uni_array;
unset($uni_array);

- then I retrieve some info again, and so on
$uni_array[] = ("14");
$uni_array[] = ("blabi");
$uni_array[] = ("blabloa");
$multi_array[] = $uni_array;
unset($uni_array)

I need to do that in order to sort the arrays by the first value (the integer) afterwards.
What is happening is that I have got nothing in the $multi_array at the end.
I think the problem is coming from $multi_array[] = $uni_array; but I don't know how to fix it, or maybe there is a better way to do what I want to do.

Thanks for the help.

Re: Problem using multidimensional array whithout initialisation

Posted: Wed Feb 17, 2010 6:05 pm
by AbraCadaver
Works great for me after I fix the parse error because of the missing ; on the last unset() line:

Code: Select all

//- first I initialize the arrays
$multi_array = array(array());
$uni_array = array();
 
//- then I need to retrieve some info on the web, that I want to store in an array
$uni_array[] = ("1");
$uni_array[] = ("blabla");
$uni_array[] = ("blablabla");
 
//- then I want to store it in the multidimensional array, to be able to use it after
// I know that I need to copy the $uni_array and think the problem is here (tried to do it but never get it worked)
$multi_array[] = $uni_array;
unset($uni_array);
 
//- then I retrieve some info again, and so on
$uni_array[] = ("14");
$uni_array[] = ("blabi");
$uni_array[] = ("blabloa");
$multi_array[] = $uni_array;
unset($uni_array);
 
print_r($multi_array);
You'll end up with an extra empty array in the first element so you want to change $multi_array = array(array()); to $multi_array = array();

Code: Select all

Array
(
    [0] => Array
        (
        )
 
    [1] => Array
        (
            [0] => 1
            [1] => blabla
            [2] => blablabla
        )
 
    [2] => Array
        (
            [0] => 14
            [1] => blabi
            [2] => blabloa
        )
)

Re: Problem using multidimensional array whithout initialisation

Posted: Thu Feb 18, 2010 8:04 pm
by mebibou
hum well that's weird ... really don't work on my website!