Array Merging

I’ve looked through the built in array functions and I can’t seem to find something to do what I want.

I have two arrays. One is built from a MySQL many-to-many relationship result set that looks like this:


Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Jeff
            

=>
)

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

    [2] => Array
        (
            [id] => 3
            [name] => Bob
            

=> SBP
)

[3] => Array
    (
        [id] => 3
        [name] => Bob
 => SB
        )

    [4] => Array
        (
            [id] => 3
            [name] => Bob
            

=> PB
)
)



The second is a list of the codes table:

```php

Array
(
    [0] => Array
        (
            

=> SBP
)

[1] => Array
    (
 => SB
        )

    [2] => Array
        (
            

=> PB
)

[3] => Array
    (
 => OSB
        )
)

What I want to end up with is one array that merges all of the codes into the keys for each person and if that person has a code then it assigns a value of ‘Y’. For example, how do I go about getting my array to look exactly like this:


Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Jeff
            [SBP] => 
            [SB] => 
            [PB] => 
            [OSB] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => Dan
            [SBP] => 
            [SB] => 
            [PB] => 
            [OSB] => 
        )

    [2] => Array
        (
            [id] => 3
            [name] => Bob
            [SBP] => Y
            [SB] => Y
            [PB] => Y
            [OSB] => 
        )
)

That’s gonna be ugly. Does it need to be a numerical array?

foreach array1
if !isset out[array1item[id]]
create empty output array entry based on array1item[id].
if !empty array1item


    out[array1item[id]][array1item

] = “Y”
endforeach
sort $out.

I just need to be able to display it in an HTML table like this:


foreach ($persons as $person) {
echo '<td>' . $person['id'] . '</td>';
echo '<td>' . $person['name'] . '</td>';
    foreach ($codes as $code) {
        echo '<td>' . $person[$code['code']] . '</td>';
    }
}

then skip the sort in the above pseudocode and use that.


<?php
$a = array(
  array('id' => 1, 'foo' => 'foo'),
  array('id' => 2, 'bar' => 'bar')
);

$b = array(
  array('age' => 10),
  array('age' => 20)
);

$entries = array();

foreach(array_map(null, $a, $b) as $entry){
  $entries[] = array_merge($entry[0], $entry[1]);
}

print_r($entries);

/*
Array
(
  [0] => Array
  (
    [id] => 1
    [foo] => foo
    [age] => 10
  )
  [1] => Array
  (
    [id] => 2
    [bar] => bar
    [age] => 20
  )
)
*/

except what he wants to do is effectively create an object structure from array2 and use it to compress array1. He doesnt actually want to merge array1 and array2.

Sorry, I don’t understand this.

Which part?

Got it working, thanks.