1

I have an array like this in php

Array
(
    [0] => Array
        (
            [month] => April-2014
            [total_booking] => 2
        )

    [1] => Array
        (
            [month] => May-2014
            [total_booking] => 5
        )

    [2] => Array
        (
            [month] => June-2014
            [total_booking] => 25
        )

    [3] => Array
        (
            [month] => October-2013
            [total_booking] => 1
        )

    [4] => Array
        (
            [month] => July-2014
            [total_booking] => 4
        )

)

I have to make this array

if i selected two months from_month , to_month if there are no any months in this array it should be included eg:

i have selected from_month 2014 feb to_month 2014 may . but if in my array only 2014 feb, 2014 april,2014 may only so iwat to include 2014 march in correct place. like this

 Array
            (
                [month] => March-2014
                [total_booking] => 0
            )

this is my code

 foreach ($newarray as $month => total_booking) {
         //sorting 

      }



     foreach ($newarray as $month => total_booking) {
             //if there is no month in array betwean to_month and from_month it    

            should be  included in correct place as sorted

          }
2
  • You are asking how to sort it? Commented Jul 10, 2014 at 9:52
  • @putvande no..,,,..... Commented Jul 10, 2014 at 10:03

2 Answers 2

1

The best way is to take reference from the start date and end date you inputted and look it up in the master array..

Here it is(please scroll for answer)

$your_array=array
   (
     array
        (
            'month' => 'April-2014',
            'total_booking' => 2
        ),

    array
        (
            'month' => 'May-2014',
            'total_booking' => 5
        ),

    array
        (
            'month' => 'June-2014',
            'total_booking' => 25
        ),

    array
        (
            'month' => 'October-2013',
            'total_booking' => 1
        ),

    array
        (
            'month' => 'July-2014',
            'total_booking' => 4
        )

);
        $start_date="Jan 1 2013";
        $end_date="Dec 31 2014";
        $timestamp1=strtotime($start_date);
        $timestamp2=strtotime($end_date);
        for($i=$timestamp1;$i<$timestamp2;$i=$i+24*60*60){
            //echo $i;
            $gapmonth[]=date('F-Y',$i);
        }
        $gapmonth=array_unique($gapmonth);

        //convert $dataS(ORIGINAL ARRAY) to one dimentional array so the life will be easier
        foreach($your_array as $val){
            $derived_val[$val['month']]=$val['total_booking'];
        }

        foreach($gapmonth as $val){
            if(array_key_exists($val,$derived_val)){
                $total_booking=$derived_val[$val];
            }
            else{
                $total_booking=0;
            }
            $finaldate[]=array('month'=>$val,'total_booking'=>$total_booking);
        }
        echo "<pre>";
        print_r($finaldate);

OUTPUTS

Array
(
    [0] => Array
        (
            [month] => January-2013
            [total_booking] => 0
        )

    [1] => Array
        (
            [month] => February-2013
            [total_booking] => 0
        )

    [2] => Array
        (
            [month] => March-2013
            [total_booking] => 0
        )

    [3] => Array
        (
            [month] => April-2013
            [total_booking] => 0
        )

    [4] => Array
        (
            [month] => May-2013
            [total_booking] => 0
        )

    [5] => Array
        (
            [month] => June-2013
            [total_booking] => 0
        )

    [6] => Array
        (
            [month] => July-2013
            [total_booking] => 0
        )

    [7] => Array
        (
            [month] => August-2013
            [total_booking] => 0
        )

    [8] => Array
        (
            [month] => September-2013
            [total_booking] => 0
        )

    [9] => Array
        (
            [month] => October-2013
            [total_booking] => 1
        )

    [10] => Array
        (
            [month] => November-2013
            [total_booking] => 0
        )

    [11] => Array
        (
            [month] => December-2013
            [total_booking] => 0
        )

    [12] => Array
        (
            [month] => January-2014
            [total_booking] => 0
        )

    [13] => Array
        (
            [month] => February-2014
            [total_booking] => 0
        )

    [14] => Array
        (
            [month] => March-2014
            [total_booking] => 0
        )

    [15] => Array
        (
            [month] => April-2014
            [total_booking] => 2
        )

    [16] => Array
        (
            [month] => May-2014
            [total_booking] => 5
        )

    [17] => Array
        (
            [month] => June-2014
            [total_booking] => 25
        )

    [18] => Array
        (
            [month] => July-2014
            [total_booking] => 4
        )

    [19] => Array
        (
            [month] => August-2014
            [total_booking] => 0
        )

    [20] => Array
        (
            [month] => September-2014
            [total_booking] => 0
        )

    [21] => Array
        (
            [month] => October-2014
            [total_booking] => 0
        )

    [22] => Array
        (
            [month] => November-2014
            [total_booking] => 0
        )

    [23] => Array
        (
            [month] => December-2014
            [total_booking] => 0
        )

)
Sign up to request clarification or add additional context in comments.

3 Comments

I suggest you delete your array in the first codeblock and use a name like $your_array, I almost missed that you actually had code (I though you did it by hand;) )
SORRY SIR .. I DIDNT GET U .. I AM POOR IN ENGLISH :)
I did it myself. Please use lowercase.
1

Loop through it and test if values are missing:

$prev_month = false;
foreach($your_array as $k=>$values){
    if($prev_month!==false){ // first itteration will be empty, dont do useless checks
        // Test if themonth after the previous month matches this one
        if( $prev_month+1 !== $values['month']  ){
            // FILL IN THE BLANKS
            // splice in at position $thisIndex (<- this you'll have to do)
            array_splice( $prev_month, $thisIndex-1, 0, $newDateBookingsArray); // -1, we want it before this item!
            // (dont forget to loop in case more than 1 items miss)
        }

    }
    $prev_month = $values['month']; // save value for next round
}

Edit: TS first asked to sort the array, the code to use a custom sort can be found in the edits of this post.

4 Comments

actually this is not my requirment i want to include all months betwean this date as sorted. for eg: i have selected from_month 2014 feb to_month 2014 may . but if in my array only 2014 feb, 2014 april,2014 may only so iwat to include 2014 march in correct place
You asked to "1. make this array sorted". The 2nd part of my answer answers part 2 of your question
that means i want to include as sorted. my mistake..my qn was confusing.. edited my qn
I've editted my post. Included a method to add a value at the point you want, this way resroting isnt needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.