beanymanuk Posted November 1, 2011 Share Posted November 1, 2011 Hi everyone I have 2 queries which are pulling out results into arrays I think they are right I then insert a new column into each array one with [typeofdonation] as "cash" and one as "card" this is to be used later when outputting the results I then need to combine the 2 arrays into 1 array and then sort the combined array by date showing the latest ones first I then need to output the top 3 results The way the results are outputted is dependant on what [typeofdonation] is as they are outputting and styled differently Here is my code below I have got upto the combining the 2 arrays but am having difficulty doing this Once I have done this I then need to sort the results but I think my code I have tested on each array works ok and so will work once I have the combined array. Final part I am not sure on how to find and use what is in [typeofdonation] to determine the styling and way each of the 3 results is outputted Thankyou in advance for everyone who can hopefully help me in the right direction Peter <b>Card Donations</b><br><br> <?php $getcarddonations = $wpdb->get_results("SELECT wp_supporters_donations.SFMemberNumber, wp_supporters_donations.KickbackAmount, wp_supporters_donations.MerchantName, wp_supporters_donations.SpendDate from supporter_2_cause RIGHT JOIN wp_supporters_donations ON supporter_2_cause.supp_id=wp_supporters_donations.SFMemberNumber WHERE supporter_2_cause.caus_id = '54' ORDER BY wp_supporters_donations.SpendDate"); //UNION SELECT id, amout, cause, date FROM cashdepo WHERE cause='Lisas charity' AND status='2' ORDER BY id DESC foreach($getcarddonations as $getcarddonations){ $i = 0; while($i < 100) { $data[$i]['member'] = $getcarddonations->SFMemberNumber; $data[$i]['ammount'] = $getcarddonations->KickbackAmount; $data[$i]['merchant'] = $getcarddonations->MerchantName; $data[$i]['date'] = $getcarddonations->SpendDate; $data[$i]['typeofdonation'] = 'card'; $i++; } } function compareItems($a, $b) { if ( $a->date < $b->date ) return -1; if ( $a->date > $b->date ) return 1; return 0; // equality } uasort($data, "compareItems"); print_r( $data ); ?> <br><br><br><b>Cash Donations</b><br><br> <?php $getcashdonationsdb = $wpdb->get_results("SELECT * FROM cashdepo WHERE cause='Lisas charity' AND status='2' ORDER BY id DESC"); foreach($getcashdonationsdb as $getcashdonationsdb){ $i = 0; while($i < 11) { $dataa[$i]['member'] = $getcashdonationsdb->id; $dataa[$i]['ammount'] = $getcashdonationsdb->amout; $dataa[$i]['merchant'] = $getcashdonationsdb->cause; $dataa[$i]['date'] = $getcashdonationsdb->date; $dataa[$i]['typeofdonation'] = 'cash'; $i++; } } uasort($dataa, "compareItems"); print_r( $dataa ); ?> <br><br><br> <b>Combined</b><br><br> <?php $datar1 = ($data); $datar2 = ($dataa); $finalarray = array_combine($datar1, $datar2); //sort uasort($finalarray, "date"); //Print the output print_r($finalarray); ?> Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/ Share on other sites More sharing options...
msaz87 Posted November 1, 2011 Share Posted November 1, 2011 array_merge for combining the two. What are you trying to do for the output? Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/#findComment-1284073 Share on other sites More sharing options...
xyph Posted November 1, 2011 Share Posted November 1, 2011 Read up on usort for basic, or array_multisort for complex sorting. Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/#findComment-1284085 Share on other sites More sharing options...
AbraCadaver Posted November 1, 2011 Share Posted November 1, 2011 You don't need those loops if $wpdb->get_results() returns an array. Just use aliases in your query and your array will look the way you want it. You can also add the card or cash like so: SELECT 'card' AS typeofdonation, wp_supporters_donations.SFMemberNumber AS member . . . Then just array_merge() the two and run a sort(). Actually, you might be able to use a UNION to combine both queries. Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/#findComment-1284091 Share on other sites More sharing options...
beanymanuk Posted November 2, 2011 Author Share Posted November 2, 2011 array_merge for combining the two. What are you trying to do for the output? I am trying to sort my results by date to show the latest 3 I have now combined all the results but I am not getting the latest 3 results outputted so I think I am sorting my array incorrectly. Any ideas how I sort by ['date'] so the latest 3 show? This is code I am using to sort uasort($data, "compareItems"); function compareItems($a, $b) { if ( $a->date < $b->date ) return -1; if ( $a->date > $b->date ) return 1; return 0; // equality } Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/#findComment-1284230 Share on other sites More sharing options...
AbraCadaver Posted November 2, 2011 Share Posted November 2, 2011 Not tested but should work: foreach($data as $key => $value) { $temp[$key] = $value['date']; } array_multisort($temp, SORT_DESC, $data); $latest_three = array_slice($data, 0, 3); Link to comment https://forums.phpfreaks.com/topic/250251-combining-2-arrays-and-sorting-them-and-outputting/#findComment-1284267 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.