Renegade605 Posted August 14, 2008 Share Posted August 14, 2008 So I have a PHP script which pulls data from 12 different MySQL tables, multiple rows can be selected from each table. Every table has a field called 'Date'.What I want to do is take all the arrays generated by mysql_fetch_assoc() and merge them into one. Example:$mysql_table1_data = array( ["Date"]=>"2008-05-15", Other Data...)$mysql_table2_data = array( ["Date"]=>"2008-06-15", Other Data...)BECOMES... $mysql_merge_data = array ( [0]=>array( ["Date"]=>"2008-05-15", Other Data... ), [1]=>array( ["Date"]=>"2008-06-15", Other Data... ) ) So before I go and write the very long and complicated code I have in mind; does anyone have a relatively easy way of doing this?Any thoughts appreciated.Renegade Link to comment Share on other sites More sharing options...
Synook Posted August 14, 2008 Share Posted August 14, 2008 Why don't you use UNION?Edit: Oh, I suppose that wouldn't be possible if there were different values for each table... Link to comment Share on other sites More sharing options...
justsomeguy Posted August 14, 2008 Share Posted August 14, 2008 If you have a result set and you want all results in an array you can just push each result onto your array. $ar = array();while ($row = mysql_fetch_assoc($result)) $ar[] = $row; // or array_push($ar, $row); Is that what you were asking? You can change anything in the row before you add it to the array. I've got this loop that uses a database class, but it's pretty much the same thing, where it gets a result set and loops through it to change some of the values (e.g. active from 1/0 to yes/no) before adding the results to the $response array. The outer loop gets each category, and the inner loop gets each piece of content in the category and adds it to the 'children' array for the category before the whole category gets put on the $response array. $response = array(); $db->sql('SELECT id, title, active FROM content_categories ORDER BY disp_order, title'); $cats = $db->select(); foreach ($cats as $cat) { $cat['active'] = $cat['active'] ? 'Yes' : 'No'; $cur_cat = array( 'db_id' => $cat['id'], 'title' => $cat['title'], 'code' => '', 'type' => '', 'post_date' => '', 'active' => $cat['active'], 'uiProvider' => 'col', 'children' => array() ); $db->sql('SELECT id, title, code, type, active, post_date FROM content WHERE category=%d ORDER BY disp_order, title'); $db->add_param($cat['id'], false); $content = $db->select(); foreach ($content as $c) { $c['post_date'] = date($config['timestamp'], $c['post_date']); $c['active'] = $c['active'] ? 'Yes' : 'No'; $c['type'] = $config['content_types'][$c['type']]; $cur_cat['children'][] = array( 'db_id' => $c['id'], 'title' => $c['title'], 'code' => $c['code'], 'type' => $c['type'], 'post_date' => $c['post_date'], 'active' => $c['active'], 'uiProvider' => 'col', 'leaf' => true ); } $response[] = $cur_cat; } Link to comment Share on other sites More sharing options...
Renegade605 Posted August 17, 2008 Author Share Posted August 17, 2008 no that wouldn't work because I'm getting data from multiple queries (to multiple tables), not just oneAn expanded example: $mysql_table1_data = array( [0]=>array( ['Date']=>"2008-08-10", Other Data... ), [1]=>array( ['Date']=>"2008-08-22", Other Data... ) )$mysql_table2_data = array( [0]=>array( ['Date']=>"2008-08-15", Other Data... ) )$mysql_table3_data = array( [0]=>array( ['Date']=>"2008-07-29", Other Data... ), [1]=>array( ['Date']=>"2008-08-11", Other Data... ), [2]=>array( ['Date']=>"2008-08-25" Other Data... ) )/* CODE TO MERGE THE ARRAYS */$mysql_merge_data = array(---------------------------------------[0]=>array( ['Date']=>"2008-07-29", Other Data... ),---------------------------------------[1]=>array( ['Date']=>"2008-08-10", Other Data... ),---------------------------------------[2]=>array( ['Date']=>"2008-08-11", Other Data... ),---------------------------------------[3]=>array( ['Date']=>"2008-08-15", Other Data... ),---------------------------------------[4]=>array( ['Date']=>"2008-08-22", Other Data... ),---------------------------------------[5]=>array( ['Date']=>"2008-08-25", Other Data... )------------------------------------) Link to comment Share on other sites More sharing options...
justsomeguy Posted August 17, 2008 Share Posted August 17, 2008 Sounds like you just want to use array_merge followed by usort to sort by the date field. Link to comment Share on other sites More sharing options...
Renegade605 Posted August 17, 2008 Author Share Posted August 17, 2008 but doesn't the array_merge() function overwrite array elements with the same index? which would result in (in the example above) the merged array being exactly the same as the $mysql_table3_data array. correct me if I'm wrong but I don't think that will work. Link to comment Share on other sites More sharing options...
boen_robot Posted August 17, 2008 Share Posted August 17, 2008 but doesn't the array_merge() function overwrite array elements with the same index? which would result in (in the example above) the merged array being exactly the same as the $mysql_table3_data array. correct me if I'm wrong but I don't think that will work.From array_merge()'s documentation:If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. And since all of your 'Date' keys are inside numeric arrays, array_merge() is exactly what you need. Link to comment Share on other sites More sharing options...
Renegade605 Posted August 17, 2008 Author Share Posted August 17, 2008 Oops, I didn't realize that. array_merge() didn't sort my array though, is there and easy function for this? Or am I right back where I started? $mysql_table1_data = array(array("Date"=>"2008-08-10","Data"=>"a"),array("Date"=>"2008-08-22","Data"=>"b"));$mysql_table2_data = array(array("Date"=>"2008-08-15","Data"=>"c"));$mysql_table3_data = array(array("Date"=>"2008-07-29","Data"=>"d"),array("Date"=>"2008-08-11","Data"=>"e"),array("Date"=>"2008-08-25","Data"=>"f"));print_r(array_merge($mysql_table1_data,$mysql_table2_data,$mysql_table3_data)); Array ( [0] => Array ( [Date] => 2008-08-10 [Data] => a ) [1] => Array ( [Date] => 2008-08-22 [Data] => b ) [2] => Array ( [Date] => 2008-08-15 [Data] => c ) [3] => Array ( [Date] => 2008-07-29 [Data] => d ) [4] => Array ( [Date] => 2008-08-11 [Data] => e ) [5] => Array ( [Date] => 2008-08-25 [Data] => f ) ) Link to comment Share on other sites More sharing options...
boen_robot Posted August 17, 2008 Share Posted August 17, 2008 Did you tried to use usort() as already suggested by justsomeguy?You'll need to write your own custom callback function that will know how to sort the values. Read the documentation for details. Link to comment Share on other sites More sharing options...
Renegade605 Posted August 17, 2008 Author Share Posted August 17, 2008 ah, I missed that suggestion. Thanks to both of you, it works now.Now I only need to get my login script working... Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.