<?php
$original = [
1=>[
1=>['brand'=> 'bmw' , 'color'=> 'white'],
171=>['brand'=> 'audi' , 'color'=> 'black'],
172=>['brand'=> 'volvo' , 'color'=> 'black'],
175=>['brand'=> 'citroen' , 'color'=> 'green']
],
129=>[
201=>['brand'=> 'volkswagen' , 'color'=> 'grey'],
206=>['brand'=> 'bentley' , 'color'=> 'grey'],
209=>['brand'=> 'mazda' , 'color'=> 'blue'],
],
];
$filterOnArray = [172,175,209];
$finalArray = [];
foreach($original as $key=>$orig ){
$nonfiter =$orig;
foreach($filterOnArray as $filterOnArr){
if(isset($orig[$filterOnArr])){
$finalArray[$key][$filterOnArr] = $orig[$filterOnArr];
unset($nonfiter[$filterOnArr]);
}
}
$finalArray[$key] = $finalArray[$key] +$nonfiter;
}
print_r($finalArray);
- Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- Array
(
[1] => Array
(
[172] => Array
(
[brand] => volvo
[color] => black
)
[175] => Array
(
[brand] => citroen
[color] => green
)
[1] => Array
(
[brand] => bmw
[color] => white
)
[171] => Array
(
[brand] => audi
[color] => black
)
)
[129] => Array
(
[209] => Array
(
[brand] => mazda
[color] => blue
)
[201] => Array
(
[brand] => volkswagen
[color] => grey
)
[206] => Array
(
[brand] => bentley
[color] => grey
)
)
)
preferences:
94.26 ms | 411 KiB | 5 Q