. $poorly_merged . , , . (ints/strings). , , , .
:
<?php
$poorly_merged=array(
array("id"=>"5","unit"=>1),
array("id"=>"3","text"=>"three"),
array("id"=>"1","text"=>"one"),
array("id"=>"2","text"=>"two"),
array("id"=>"4","text"=>"four"),
array("id"=>"5","text"=>"five"),
array("id"=>"3","unit"=>0),
array("id"=>"1","unit"=>"0"),
array("id"=>"2","unit"=>0),
array("id"=>"4","unit"=>"0")
);
$ids_array=array_unique(array_column($poorly_merged,"id"));
sort($ids_array);
$x=0;
foreach($ids_array as $id){
$well_merged[$x]=array();
$q=array("id"=>$id);
$qualifying_array=array_filter(
$poorly_merged,
function($val)use($q){
if(sizeof(array_intersect_assoc($val,$q))==sizeof($q)){
return $val;
}
},
ARRAY_FILTER_USE_BOTH
);
foreach($qualifying_array as $to_be_flattened){
$well_merged[$x]=array_merge($well_merged[$x],$to_be_flattened);
}
++$x;
}
echo "<pre>";
var_export($well_merged);
echo "</pre>";
:
array (
0 =>
array (
'id' => '1',
'text' => 'one',
'unit' => '0',
),
1 =>
array (
'id' => '2',
'text' => 'two',
'unit' => 0,
),
2 =>
array (
'id' => '3',
'text' => 'three',
'unit' => 0,
),
3 =>
array (
'id' => '4',
'text' => 'four',
'unit' => '0',
),
4 =>
array (
'id' => '5',
'unit' => 1,
'text' => 'five',
),
)