0

I have two arrays each coming from different DB queries, I can't join them in DB so I have to join them using php. The first:

Array
(
    [0] => Array
        (
            [id] => 23
            [host_id] => 5
            [pos_id] => 2
            [status] => 1
        )

    [1] => Array
        (
            [id] => 25
            [host_id] => 5
            [pos_id] => 1
            [status] => 1
        )

    [2] => Array
        (
            [id] => 24
            [host_id] => 5
            [pos_id] => 2
            [status] => 1
        )

)

And I wanna join it with this array on pos_id and id

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Front
        )

    [1] => Array
        (
            [id] => 2
            [name] => Back
        )
)

so that, the result is:

Array
(
    [0] => Array
        (
            [id] => 23
            [host_id] => 5
            [pos_id] => 2
            [name] => Back
            [status] => 1
        )

    [1] => Array
        (
            [id] => 25
            [host_id] => 5
            [pos_id] => 1
            [name] => Front

            [status] => 1
        )

    [2] => Array
        (
            [id] => 24
            [host_id] => 5
            [pos_id] => 2
            [name] => Back
            [status] => 1
        )

)

The code I have uses an inner loop, matches the value and pushes into array:

foreach($events as &$event) {
    foreach($positions as $position) {
        if($player[pos_id] == $position[id]){
            array_push($event,"name",$position[name]);
        }
    }
}
Khaled
  • 8,255
  • 11
  • 35
  • 56
  • Maybe you have some code? Or just waiting for us to write you code for free? – u_mulder May 26 '18 at 20:27
  • I don't need someone to write me a code, I can easily make a code using a loop with inner loop to match and join both. I'm just wondering if there's a better way that I am missing with a lower time complexity. – Khaled May 26 '18 at 20:33
  • Btw, how is `pos_id = 0` connected with `id = 1`? – u_mulder May 26 '18 at 20:34
  • Sorry that was a dummy code, fixed it to match id – Khaled May 26 '18 at 20:36
  • 1
    (quote:) 'I can easily make a code using a loop with inner loop' -- rewrite second array as an associative array first (`[ '1' => 'Front', '2' => 'Back' ]`), then loop over the first array and add the appropriate names. Saves you an inner loop, resulting in `N + M` operations instead of `N * M` – Peter van der Wal May 26 '18 at 20:44
  • Show how you fetch the rows, that's where you should do it. – AbraCadaver May 26 '18 at 20:47
  • I have added my sample code – Khaled May 26 '18 at 20:54
  • Check this out https://stackoverflow.com/questions/19109815/how-to-join-two-multidimensional-arrays-in-php – Ahmed Soliman May 26 '18 at 20:59
  • @PetervanderWal while this has a better complexity, it would require that I firstly loop the second array to covert it into associative array and then join the arrays. – Khaled May 26 '18 at 21:06
  • @ahmed.soli I believe that the question addresses merging two arrays rather than joining. – Khaled May 26 '18 at 21:06
  • 1
    Yes, that's my point indeed. But your second foreach will be a much cleaner and faster one than the one you have now: `foreach($events as &$event) { $event['name'] = $positions[ $event['pos_id'] ]; }` – Peter van der Wal May 26 '18 at 21:09
  • You can loop over the first array using for loop and inside the loop make the validation using the two arrays if value of the post id of the first equal to value if id of the second then add the name to the first array i can write the code if you want – Ahmed Soliman May 26 '18 at 21:10
  • @ahmed.soli thank you, that's pretty much the sample code I have written, I will go with Peter's suggestion using associative array. – Khaled May 26 '18 at 21:25

0 Answers0