Jump to content

retrieving values from a multidimensional array...


mkosmosports

Recommended Posts

I have this array called $data.
Array
(
    [3] => Array
        (
            [0] => 200701
            [1] => 200609
            [2] => 200608
            [3] => 200607
            [4] => 200606
            [5] => 200605
            [6] => 200603
            [7] => 200601
            [8] => 200512
            [9] => 200508
        )

    [1] => Array
        (
            [0] => 200611
            [1] => 200610
            [2] => 200609
            [3] => 200608
            [4] => 200607
            [5] => 200606
            [6] => 200605
            [7] => 200602
            [8] => 200601
            [9] => 200509
            [10] => 200508
        )
)

Now, if I go echo(max($date[1]) that will bring me out the maximum number where the subarray is 1. How can I get the maximum number regardless of the subarray though. So just in general?

Thanks in advance.
I think you will have to loop through your subarrays one at a time and compare the max() value to the previously stored value and overwrite if it is higher
$max1="0"
$max2="0"
for(loop through array){
for(loop through subarray){
max1=max($date[$counter_subarray];)
if(max1>max2){
  max2=max1;
  }
}
}

hope this will put you on the right track
Perfect! That works for retrieving the max number across all subarrays anatek.
Nnnnow though, I have a brain teasing question I just cant seem to answer by myself. I think Ive jumped into working with arrays at a level a little too advanced to me, so I seek your help.....

This md array:
Array
(
[3] => Array
(
  [0] => 200701
  [1] => 200609
  [2] => 200608
  [3] => 200607
  [4] => 200606
  [5] => 200605
  [6] => 200603
  [7] => 200601
  [8] => 200512
  [9] => 200508
)
[1] => Array
(
    [0] => 200611
    [1] => 200610
    [2] => 200609
    [3] => 200608
    [4] => 200607
    [5] => 200606
    [6] => 200605
    [7] => 200602
    [8] => 200601
    [9] => 200509
    [10] => 200508
)
)

What I want to do with it is get all distinct years from a given subarray, and all of of the months available for those years. So for subarray[3] I want to retrieve 2005(12,08), 2006(09,08,07,06,05,03,01), 2007(01). I also want to do this regardless of what the subarray associative id is. The reason for this is, on another page I want to run a loop generating the months available if year is a certain value. Also, I want a loop generating links to the remaining available years, with the maximum month available in that year. Anyway, I hope I didnt confuse y'all too much. Is this even possible? I want to do this to avoid running more sql queries than I need to, is it worth it?

Any idea welcome!
Thanks.
Hmm. Maybe I should start off a little easier.. ;D

How can I parse through all second-level arrays and retrieve all of the values from there. I guess if I ran loops through all of them that could do it, but are there any quicker ways?

Thanks.
hello mkosmosports,

not so difficult (i think)
you have to loop through every array separatly and just select the years and dates using substr()
and put those values in a new array. Before you add a year to an array you must check if it already exists in that array.

on your second question
I guess if I ran loops through all of them that could do it, but are there any quicker ways?

I think there are some array functions to get all the values in one time but it depends what you want to do with it.
you can print all the values using
print_r($array)
and another function is explode() I think.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.