3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
25.16 ms | 404 KiB | 5 Q