-3

I have two arrays of assoc arrays:

first one:

Array
(
    [0] => Array
        (
            [24] => s
            [23] => czarny
        )

    [1] => Array
        (
            [24] => m
            [23] => czarny
        )

    [2] => Array
        (
            [24] => l
            [23] => czarny
        )

)

and second:

Array
(
    [0] => Array
        (
            [24] => l
            [23] => czarny
        )

    [1] => Array
        (
            [23] => czarny
            [24] => m
        )

)

in output I would like to get:

Array
(
      [24] => s
      [23] => czarny
)

because two of arrays:

[0] => Array
        (
            [23] => czarny
            [24] => m
        )

and

[0] => Array
        (
            [24] => m
            [23] => czarny
        )

are the same for me. Does anyone have idea how to deal with that? I tried to do this which fourth nested foreach but I got confused..

Mariusz Jucha
  • 141
  • 1
  • 3
  • 11

1 Answers1

0

This is a little loop crazy, but it will work.

$array1[0][23] = 'czarny';
$array1[0][24] = 'm';

$array1[1][23] = 'czarny';
$array1[1][24] = 's';

$array1[2][23] = 'czarny';
$array1[2][24] = 'l';

$array2[0][23] = 'czarny';
$array2[0][24] = 'm';

$array2[1][23] = 'czarny';
$array2[1][24] = 'l';

end($array1);

// Merge two arrays
$array3 =   array_merge($array1,$array2);
// Loop through the one array and create a second assoc array based on key 24
foreach($array1 as $mainKey => $object) {
        foreach($object as $key => $value) {
                $newArray[$object[24]][$key] = $value;
            }
    }
// Loop through new array to create yet another new one but reset keys back to numeric
$i=0;
foreach($newArray as $key => $value) {
        $final[$i]  =   $value;

        $i++;
    }

print_r($final);
Rasclatt
  • 12,498
  • 3
  • 25
  • 33