iwpg Posted March 7, 2014 Share Posted March 7, 2014 (edited) Just dont know where to start, any guidance is greatly appreciated. I have a charting program that can produce 2 array sets for different data and print it using the same time period/date. The problem that I have is there are some queries that produce missing dates (there is no data in this case). This shifts the array over into the wrong date. I added the dates to the array to compare, but I really don't know what to do with it. I thought about using array_merge, but Data1 needs to check Data2 values, and then backwards so the dates match (Year-Month). I know the array is a mess due to this, and re-writing it may be out of the question since there are thousands of records with similar info. Data 1:Array ( [2012-04] => 14869 [2012-06] => 3214 [2012-07] => 5686 [2012-08] => 3937 [2012-09] => 4375 [2012-10] => 3049 [2012-11] => 2141 [2012-12] => 1685 [2013-01] => 1697 [2013-02] => 125 [2013-03] => 155 [2013-06] => 235 [2013-07] => 171 [2013-08] => 202 [2013-09] => 15 [2013-10] => 463 [2013-11] => 846 [2014-01] => 2617 ) Data 2:Array ( [2012-03] => 77250 [2012-04] => 66318 [2012-05] => 47291 [2012-06] => 54074 [2012-07] => 59331 [2012-08] => 65867 [2012-09] => 60705 [2012-10] => 67843 [2012-11] => 59960 [2012-12] => 58008 [2013-01] => 69416 [2013-02] => 60929 [2013-03] => 52830 [2013-04] => 47863 [2013-05] => 41873 [2013-06] => 37582 [2013-07] => 43731 [2013-08] => 45229 [2013-09] => 4486 [2013-10] => 45473 [2013-11] => 45706 [2013-12] => 54593 [2014-01] => 66230 ) A little more clarification, both arrays should have matching dates, if either is missing dates, the date should be taken from the other array and assigned 0 for it. This is what I would like the end result to be: Data 1:Array ( [2012-03] => 0 [2012-04] => 14869 [2012-05] => 0 [2012-06] => 3214 [2012-07] => 5686 [2012-08] => 3937 [2012-09] => 4375 [2012-10] => 3049 [2012-11] => 2141 [2012-12] => 1685 [2013-01] => 1697 [2013-02] => 125 [2013-03] => 155 [2013-04] => 0 [2013-05] => 0 [2013-06] => 235 [2013-07] => 171 [2013-08] => 202 [2013-09] => 15 [2013-10] => 463 [2013-11] => 846 [2013-12] => 0 [2014-01] => 2617 ) Data 2:Array ( [2012-03] => 77250 [2012-04] => 66318 [2012-05] => 47291 [2012-06] => 54074 [2012-07] => 59331 [2012-08] => 65867 [2012-09] => 60705 [2012-10] => 67843 [2012-11] => 59960 [2012-12] => 58008 [2013-01] => 69416 [2013-02] => 60929 [2013-03] => 52830 [2013-04] => 47863 [2013-05] => 41873 [2013-06] => 37582 [2013-07] => 43731 [2013-08] => 45229 [2013-09] => 4486 [2013-10] => 45473 [2013-11] => 45706 [2013-12] => 54593 [2014-01] => 66230 ) Edited March 7, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/286801-comparing-2-arrays-and-updating-to-match-dates/ Share on other sites More sharing options...
iwpg Posted March 7, 2014 Author Share Posted March 7, 2014 I'm starting to come to thoughts of a conclusion. Would it be feasible to check the first and last dates of each array, iterate through the range and then check it with strtotime + 1 month? Quote Link to comment https://forums.phpfreaks.com/topic/286801-comparing-2-arrays-and-updating-to-match-dates/#findComment-1471798 Share on other sites More sharing options...
Solution Zane Posted March 7, 2014 Solution Share Posted March 7, 2014 (edited) Using this 5 year old example, http://us3.php.net/manual/en/function.array-diff-key.php#82257 should help. Using that array_unique_diff function, you should be able to have an array of all the unique and different keys. Then you can iterate through those with a loop and add them to each array.with a value of 0. If I understand you correctly. function array_unique_diff ($array1, $array2) { return array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1)); } $a = array ( "one" => 1, "two" => 2, "three" => 3 ); $b = array ( "one" => 56, "two" => 2, "four" => 19 ); $missingKeys = array_unique_diff ( $a, $b ); foreach ( $missingKeys as $key=>$val ) { $a[$key] = 0; $b[$key] = 0; } That should change $a and $b to be $a = array ( "one" => 1, "two" => 2, "three" => 3, "four" => 0 ); $b = array ( "one" => 56, "two" => 2, "three" => 0, "four" => 19 ); Edited March 7, 2014 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/286801-comparing-2-arrays-and-updating-to-match-dates/#findComment-1471804 Share on other sites More sharing options...
iwpg Posted March 8, 2014 Author Share Posted March 8, 2014 I went through the array manuals first at php.net. Didn't catch this one. Thanks for helping me out, I truly appreciate it! Mike Quote Link to comment https://forums.phpfreaks.com/topic/286801-comparing-2-arrays-and-updating-to-match-dates/#findComment-1471838 Share on other sites More sharing options...
iwpg Posted March 8, 2014 Author Share Posted March 8, 2014 Works perfect, also added ksort() to the arrays to get them back in order. Quote Link to comment https://forums.phpfreaks.com/topic/286801-comparing-2-arrays-and-updating-to-match-dates/#findComment-1471841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.