0

I have two arrays in PHP as follows,

Resultant array:

Array
(
    [0] => Array
        (
            [month] => Dec 2016
            [count] => 2
        )

    [1] => Array
        (
            [month] => Feb 2017
            [count] => 2
        )
)

Month Array:

Array
(
    [0] => Dec 2016
    [1] => Jan 2017
    [2] => Feb 2017
)

I am trying to merging this arrays so I can get below result, but it's not happening. In this array, I using array_search() and in_array() for checking the array values is present in result array but not getting expected result. I want below output,

Array
(
    [Dec 2016] => 2
    [Jan 2017] => 0
    [Feb 2017] => 2
)

My code:

foreach ( $result_array as $val ) {
    $month = array_search ( $val ['month'], $monthArray );
    if ($val ['count'] == '' || $val ['count'] == 'NULL') {
        $countValue = 0;
    } else {
        $countValue = $val ['count'];
    }
    $final_array [] = $countValue;
}
3
  • Post your code. Commented Feb 21, 2017 at 7:58
  • foreach ( $result_array as $val ) { $month = array_search ( $val ['month'], $monthArray ); if ($val ['count'] == '' || $val ['count'] == 'NULL') { $countValue = 0; } else { $countValue = $val ['count']; } $final_array [] = $countValue; } Commented Feb 21, 2017 at 8:54
  • Put the in the question and format it properly. Use the "edit" link located below the question to update the question text any time you feel you can make it more clear (this way you increase your chances to get an useful answer.) Commented Feb 21, 2017 at 8:57

2 Answers 2

1

Considering that we have clean data:

    //turn months into keys    
    $months = array_flip($monthArray);

    // fill in the count values
    foreach($resultArray as $result) {
        $months[$result['month']] = $result['count'];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Simas. This very useful for me.I am using this and array what I want.I am getting below array ( [Dec 2016] => 2 [Jan 2017] => 1 [Feb 2017] => 2 ) see above array value set for Jan 2017 =1 but no count for Jan 2017.It should be 0.array ( [Dec 2016] => 2 [Jan 2017] => 0 [Feb 2017] => 2 )
0

A possible solution:

$result_array = array(
    array(
        'month' => 'Dec 2016',
        'count' => 2,
    ),
    array(
        'month' => 'Feb 2017',
        'count' => 2,
    )
);

$monthArray = array(
    'Dec 2016',
    'Jan 2017',
    'Feb 2017',
);



$final_array = array_combine($monthArray, array_fill(0, count($monthArray), 0));
foreach ($result_array as $item) {
    $mon = $item['month'];
    if (in_array($mon, $monthArray)) {
        $final_array[$mon] += $item['count'];
    }
}
print_r($final_array);   

How it works

It uses array_combine() to create an array that associates 0 (zero) to the values in $monthArray. It basically turns the values in $monthArray to keys into a new array that has 0 as values.

Then it iterates over $result_array and for each item, if the value of 'month' is in the output array then it simply add the value of 'count' to the output.

The items having 'month' values that are not present in the $monthArray array are not added to the output.

1 Comment

Thank u very much Axiac.

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.