Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have the following array:

    Array
    (
        [0] => Array
            (
                [year] => 1
                [month] => Array
                    (
                        [0] => Array
                            (
                                [month] => 2
                                [value] => 600
                            )

                        [1] => Array
                            (
                                [month] => 3
                                [value] => 600
                            )

                        [2] => Array
                            (
                                [month] => 4
                                [value] => 600
                            )
                    )
            )
        [1] => Array
            (
                [year] => 5
                [month] => Array
                    (
                        [0] => Array
                            (
                                [month] => 6
                                [value] => 80
                            )

                        [1] => Array
                            (
                                [month] => 7
                                [value] => 90
                            )

                        [2] => Array
                            (
                                [month] => 8
                                [value] => 100
                            )
                    )
            )
    )

and also i have a foreach for 10 years that also foreach for every month. Something like this:

foreach (range(1, 10) as $year) {
     foreach (range(1, 12) as $month) {
            $value = $month;
     }
}

Now the problem is how i can match the array with the foreach to change the $value if the $year and $month it's in the array, and if the year/month combination it's on the array the $value value needs to be the one from the that month in array.

share|improve this question
1  
Could you rephrase what you are trying to do? I don't think I understand it. – ConnectedSystems Oct 2 '14 at 11:54
up vote 0 down vote accepted

Having written the below, it is worth mentioning that the whole exercise seems a little pointless. As you are iterating through every month in a ten year period, it would be much simpler to iterate the data and check if the year matches (because no month in that year can logically be outside the range 1-12), rather than iterate through 120 months to see if the data contains a value for that month:

foreach($data as $year_data) {
    if(in_array($year_data['year'], range(1,10)){
        foreach($year['month'] as $month) {
            echo = $month['value'];
        }
    }
}

Well its not clear what you want to do with the data, but to get to it you will need to iterate the array with nested foreach loops:

foreach (range(1, 10) as $year) {
    foreach (range(1, 12) as $month) {
         //iterate the array to find the correct element
         $foundValue = false;
         foreach($data as $year_data){
            if($year_data['year']==$year){
                foreach($year_data['month'] as $month_data){
                    if($month_data['month']==$month){
                        //do something with the data
                        $foundValue = $month_data['value'];
                    }
                }
            }
        }
        echo ($foundValue) ?: $defaultValue;
    }
}

If you could restructure the data array to be indexed by year and month, eg:

$data = array(
      'year_1'=>array(
                      'month_2'=>600,
                      'month_3'=>600,
                      'month_4'=>600
                     ),
      'year_5'=>array(
                      'month_6'=>80,
                      'month_7'=>90,
                      'month_8'=>100
                     ),
     );

Then you could access the data alot easier:

foreach (range(1, 10) as $year) {
    foreach (range(1, 12) as $month) {
         //access value directly via keys:
         if(isset($data['year_'.$year]['month_'.$month];)){
             echo $data['year_'.$year]['month_'.$month];
         }else{
             echo $defaultValue;
         }
    }
}

Even if you cant alter how the data is created, you could still create a new array with the above structure:

$newData = array();

foreach($data as $year_data){
    $year = 'year_'.$year_data['year'];
    $months = array();
    foreach($year_data['month'] as $month_data){
        $months['month_'.$month_data['month']]=$month_data['value'];
    }
    $newData[$year]=$months;
}

Then you can use $newData in place of $data in the simplified example above.

share|improve this answer
    
Steve and @Dr Casper Black, thanks for your very complete answer, this works but i also need to show the default month value.` if($month_data['month']==$month){ echo $month_data['value']; }` i've tried something like ` if($month_data['month']==$month){ echo $month_data['value']; }else{echo $default_month_value;}` but this way every months will be repeated by the number of month in that array. – Alexandru Burdulea Oct 2 '14 at 12:52
    
@AlexandruBurdulea Well you need to save the value outside the inner loops, then after those loops, if the value there, echo it, else echo default. I dont know what the default should be so i used $defaultValue in my edited answer – Steve Oct 2 '14 at 13:06
    
@AlexandruBurdulea Also added break to avoid unnecessary loops if you are using the 1st example (hint - you shouldnt be, restructuring once and using the second one is more efficient) – Steve Oct 2 '14 at 13:09
    
Thanks a lot, this was exactly for what i was looking. Works exactly how i wanted. – Alexandru Burdulea Oct 2 '14 at 15:47

Your question is vague but this may help you. This will go trough your array of year/month, check if year corresponds, if yes, it will check if month matches, if yes show values.

The year and month range will match all your data-array entries.

foreach (range(1, 10) as $year) {
     foreach (range(1, 12) as $month) {
        foreach($ar as $row){
            if ($row['year'] == $year){
                foreach($row['month'] as $rowMonth){
                    if($rowMonth['month'] == $month){
                        echo "match: Y:".$row['year']." - m:".$rowMonth['month']." -> ". $rowMonth['value']."<br>"; 
                    }

                }
            }
        }
     }
}
share|improve this answer

Do you mean something like this?

$year_month = array( array( 'year' => 1, 'month' => array( array('month' => 2, 'value' => 600), array('month' => 3, 'value' => 600), array('month' => 4, 'value' => 600) ) ), array( 'year' => 5, 'month' => array( array('month' => 6, 'value' => 80), array('month' => 7, 'value' => 90), array('month' => 8, 'value' => 100) ) ) );

foreach($year_month as $year) { foreach($year['month'] as $month) { $value = $month['value']; echo $value.'<br/>'; } }

Output:

600 600 600 80 90 100

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.