Welcome Guest, Not a member yet? Register   Sign In
Merging arrays from different queries
#1

[eluser]JanDoToDo[/eluser]
Hey guys,

Im making a recent activity feed. I have multiple tables with different info, eg members, messages, alerts etc.

From each table I get the last 4 results by date, most recent first.

I want to combine each 3 arrays and then dump the whole array in date order.

How would I go about doing this? Obviously each table has different columns but each does have a date column. At the minute I loop through each array separately and then echo but that means i have all the new members in date order, then the new messages in date order and then alerts in date order. However I want the whole lot in date order not split between category.

I was thinking either have an entire new table of recent activity and then put an entry in there for everything that happens or alternatively merge the arrays some how and then re sort by date but i dont know how to do it. Having a whole new table for activity seems inefficient and is replicating data.

Ive tried merging but each array has different keys and stuff and so can anyone advise the best way to do this?
#2

[eluser]Nick_MyShuitings[/eluser]
Its late... so if this makes no sense then meh....

Code:
$array1[] = array(
'date' => 'whenever',
'stuff' => 'yeap',
'otherstuff' => 'mmhmm');
$array1[] = array(
'date' => 'whenever+1',
'stuff' => 'yeapzzz',
'otherstuff' => 'foo');
$array2 = array(
'date' => 'whenever2',
'differentstuff' => 'wow',
'morestuff' => 'zzzz');

foreach ($array1 as $array){
    $date = $array['date'];
    unset($array['date']);
    $newarray[$date] = array(
    'type' => 'array1',
    'data' => $array);
}
foreach ($array2 as $array){
    $date = $array['date'];
    unset($array['date']);
    $newarray[$date] = array(
    'type' => 'array1',
    'data' => $array);
}

repeat the foreach for each array you want to merge into this thing... and you end up with an array like this:
Code:
array(3) {
  ["whenever"]=>
  array(2) {
    ["type"]=>
    string(6) "array1"
    ["data"]=>
    array(2) {
      ["stuff"]=>
      string(4) "yeap"
      ["otherstuff"]=>
      string(5) "mmhmm"
    }
  }
  ["whenever+1"]=>
  array(2) {
    ["type"]=>
    string(6) "array1"
    ["data"]=>
    array(2) {
      ["stuff"]=>
      string(7) "yeapzzz"
      ["otherstuff"]=>
      string(3) "foo"
    }
  }
  ["whenever2"]=>
  array(2) {
    ["type"]=>
    string(6) "array2"
    ["data"]=>
    array(2) {
      ["differentstuff"]=>
      string(3) "wow"
      ["morestuff"]=>
      string(4) "zzzz"
    }
  }
}

Which you can then sort by the key. From there using the type you can display it differently on the front end if that is necessary, using a switch case within your giant foreach loop.

Now... is this elegant? No. could it prolly be done quicker and eaiser? yes. Just not at 3:30 AM and not if Godforsaken Drupal doesn't stop causing bugs as the client tries to push to live... I hope this puts you on the right path.
#3

[eluser]JanDoToDo[/eluser]
Nick,
Thanks a lot for your reply. I did it in the end with merge_array_recursive. For each of the arrays i created a new key/value pair called "sort_date" for each of the array rows (like you have done but you ahve just unset/reset the value) and then i array_merged_recrsive to get them all into one array. I thought it might not work as different array rows have different fields, i.e. some are members/threads etc however it has worked fine. The only thing ive noticed is that I have a 2d sort algorithm and if i use it to sort by the date it doesnt work so i have to convert the date into a timestamp, sort the array and then convert it back into a date! Apart from that it is all working perfectly now :-) thankyou!

Ultimately, when I make everything tidier I will just select the date column in mysql as a different name so that i dont have to do it in PHP. Then all I ahve to do is merge recursive. Cheers Nick!
#4

[eluser]juanvillegas[/eluser]
Podrias tambien ahorrarte todo eso y aprovechar MySQL: http://www.mysqltutorial.org/sql-union-mysql.aspx
#5

[eluser]Nick_MyShuitings[/eluser]
[quote author="juanvillegas" date="1283453828"]Podrias tambien ahorrarte todo eso y aprovechar MySQL: http://www.mysqltutorial.org/sql-union-mysql.aspx[/quote]

Translation: You could save yourself all that mess and just take advantage of MySQL http://www.mysqltutorial.org/sql-union-mysql.aspx.

This is very good advice on one condition: Check out this line from the doc:

Quote:The column list of each individual SELECT statement must have the same data type.

Which I am not sure if that works in their specific instance... so union might be a total win, but might be not feasible.

JanDoToDo: could you post the clean code you wrote with array_merge_recursive, so that future readers will ignore my hideous latenight example.
#6

[eluser]juanvillegas[/eluser]
Quote:Obviously each table has different columns but each does have a date column.

Well yeah, but he's got the id + date columns. He might get an array with id's ordered by date from the three columns and then traverse this new array from zero to max getting information of the items. It would be 4 lines of code at most.

Hahah i don't really know why i wrote in spanish! Thanks for the translation brother
#7

[eluser]Nick_MyShuitings[/eluser]
I'm down in Argentina for my company, and my brain always has trouble doing the switch back and forth from Spanish to English and I end up getting a lot of blank stares from some of the devs.

I kinda want JanDoToDo to post a better example of the arrays he wants to merge, so that we can see the best way to run it... nudge nudge.




Theme © iAndrew 2016 - Forum software by © MyBB