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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.