Page 1 of 1

Retreive sub-arrays in a multidimensional array. Need help.

Posted: Thu Aug 02, 2007 12:06 pm
by romain.pelissier
Hi,
I am stuck with what it seems to me a simple problem and I need some help. I am very new php programmer ... I am pretty sure that you experts can give me the answer in few words..
Here it is :

I have a multidimensional array that looks like this :

Code: Select all

Array ( [2007] => Array ( [0] => Array ( [mois] => 7 [path] => /var/www/html/rapports/ARCHIVES/2007/7 ) [1] => Array ( [mois] => 6 [path] => /var/www/html/rapports/ARCHIVES/2007/6 ) ) [2006] => Array ( [0] => Array ( [mois] => 1 [path] => /var/www/html/rapports/ARCHIVES/2006/1 ) ) )
As you can see, the indexes are a number : 2007, 2006, ... and each item of those keys contains an array with other items like a path and a number :

Code: Select all

2007
       0
           path : /var/www/html/rapports/ARCHIVES/2007/
           month : 7
       1
           path : /var/www/html/rapports/ARCHIVES/2007/
           month : 6
2006
       0
...
Now, if I know my index number (let say 2007) is there a way to retrieve the entire sub-array for that particular key so I can have in a array the values for the key I have choosed ?

I hope you could answer me.

Thanks

Posted: Thu Aug 02, 2007 12:11 pm
by VladSun

Code: Select all

$a = $array[2007][1];
You should have:
$a['path'] : /var/www/html/rapports/ARCHIVES/2007/
$a['month'] : 6

Posted: Thu Aug 02, 2007 12:16 pm
by romain.pelissier
Thanks VladSun,

In fact, I want to retreive the entire sub-array, not a particular value.

Let say my array is called $retvalue and I have this :

Code: Select all

foreach ($retval as $list=>$things) {
                if ($list = 2007) {
                        print " The year have been found in the array";
                        print "<br>\n";
                        
                 }
        }
What i want here is if the if test is successfull, to retreive the sub-array of 2007.

Posted: Thu Aug 02, 2007 12:20 pm
by romain.pelissier
Seems that a user in another forum gives me the answer :

Code: Select all

$array_2007 = $retval[2007];

print_r($array_2007);
Problem solved! Thanks again to have taken the time to help me.