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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
romain.pelissier
Forum Newbie
Posts: 18
Joined: Fri Jun 15, 2007 12:01 pm

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

$a = $array[2007][1];
You should have:
$a['path'] : /var/www/html/rapports/ARCHIVES/2007/
$a['month'] : 6
There are 10 types of people in this world, those who understand binary and those who don't
romain.pelissier
Forum Newbie
Posts: 18
Joined: Fri Jun 15, 2007 12:01 pm

Post 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.
romain.pelissier
Forum Newbie
Posts: 18
Joined: Fri Jun 15, 2007 12:01 pm

Post 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.
Post Reply