0

Starting off I have two PHP arrays.

The first array has a length of dates in Y-m order that has each of the 12 months in it with the years. This array looks like this...

Array
( 
    [0] => Array ( [ORDER] => Array ( [DATE] => 2008-10 [QTY] => 0 ) ) 
    [1] => Array ( [ORDER] => Array ( [DATE] => 2008-11 [QTY] => 0 ) ) 
)

The second array has values that have been returned from a query and put into the array and are also in Y-m order for may or may not have each month in it. The second one is like this.

Array
(
    [0] => Array ( [ORDER] => Array ( [DATE] => 2008-11 [QTY] => 285 ) )
    [1] => Array ( [ORDER] => Array ( [DATE] => 2008-12 [QTY] => 900 ) )
)   

My end goal is to produce an array of the Y-m dates to use for a google line chart that will show a 0 if in that month no orders where placed. So that array should look like this..

Array
( 
    [0] => Array ( [ORDER] => Array ( [DATE] => 2008-10 [QTY] => 0 ) ) 
    [1] => Array ( [ORDER] => Array ( [DATE] => 2008-11 [QTY] => 285 ) ) 
    [2] => Array ( [ORDER] => Array ( [DATE] => 2008-12 [QTY] => 900 ) )

)

So the array item...

[1] => Array ( [ORDER] => Array ( [DATE] => 2008-11 [QTY] => 0 ) )

was removed as there was an entry in the second array that had a value with the same date.

Now I'm not sure if I'm going about this the right way but here's what I have so far.

foreach($FIRST_ARRAY as $key => $each)
{
    foreach($each as $line)
    {
        $findMe = $line['DATE'];

            if(in_array($findMe, $SECOND_ARRAY, true))
                {   
                    echo 'FOUND:'.$findMe;

                }else{          
                    echo $line['DATE'].' ';
                    echo $line['QTY'].' ';
                    echo '('.$findMe.')<br>';
                }
    }
}

All this is doing is printing out the First Array. It's not 'Matching' up the dates. Here's what its printing out...

2008-10 0 (2008-10) 2008-11 0 (2008-11) 2008-12 0 (2008-12)

Here's what I want it to print out...

2008-10 0 (2008-10) 2008-11 285 (2008-11) 2008-12 900 (2008-12)

The Question would be: Am I going about this all wrong or is there a better way of doing this.

I hope this all makes sense.

2
  • Are these values coming from a database? You can also make a SUM in a query, no need to do this in php. Commented Oct 12, 2012 at 15:14
  • Will your two arrays be pre-sorted by yyyy-mm in ascending order? Commented Oct 12, 2012 at 15:29

2 Answers 2

1

You can try

$array = array(0 => array('DATE' => '2008-10','QTY' => 0),1 => array('DATE' => '2008-11','QTY' => 0));
$array2 = array(0 => array('DATE' => '2008-11','QTY' => 285),1 => array('DATE' => '2008-12','QTY' => 900));

$final = array();

groupAdd($final, $array, "DATE");
groupAdd($final, $array2, "DATE");

foreach ( $final as $line ) {
    echo $line['DATE'] . '&nbsp;';
    echo $line['QTY'] . '&nbsp;';
    echo '(' . $line['DATE'] . ')<br>';
}

Output

2008-10 0 (2008-10)
2008-11 285 (2008-11)
2008-12 900 (2008-12)

Function Used

function groupAdd(&$final, $array, $groupKey) {
    foreach ( $array as $item ) {
        isset($final[$item[$groupKey]]) ? $final[$item[$groupKey]]['QTY'] += $item['QTY'] : $final[$item[$groupKey]] = $item;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Wow. That was even simpler that I thought it would be. Thanks!
Now, what about if I needed more items in the array. How would I set that up?
what you you mean by more items ???? It should work if you are only dealing with DATE and QTY
It works perfect. But I'd like to add to it by adding more array values to it. Like if the arrays where like: [0] => Array ( [ORDER] => Array ( [DATE] => 2008-11 [QTY] => 285 [DISPLAYQTY] => 285 [ORDERID] => 5551231 ) )
@Monty You would need some modification because of order ID
|
0
$ans = Array();
$tmp_second_array = $SECOND_ARRAY;
foreach($FIRST_ARRAY as $key => $each){
 $arr_to_add = $each;
 if(!empty($tmp_second_array)){
  foreach($tmp_second_array as $seckey => $seceach){
   if($each['ORDER']['DATE'] === $seceach['ORDER']['DATE']){   
     $arr_to_add = $seceach;
     unset($tmp_second_array[$seckey])
     break;
   }
  }
  $ans[] = $arr_to_add;
 }           
}
if(!empty($tmp_second_array)){
 $ans = array_merge($ans, $tmp_second_array);
}

Comments

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.