这是我的初始数组:
$arr = array(
array('model'=>'123', 'garment'=>'coat', 'title'=>'aaa'),
array('model'=>'123', 'gender'=>'men', 'title'=>'aaa'),
array('model'=>'123', 'brand'=>'adidas', 'title'=>'aaa'),
array('model'=>'345', 'garment'=>'jacket', 'title'=>'bbb'),
array('model'=>'345', 'gender'=>'kids', 'title'=>'bbb'),
array('model'=>'345', 'brand'=>'nike', 'title'=>'bbb'),
array('model'=>'678', 'garment'=>'trainers', 'title'=>'ccc'),
array('model'=>'678', 'gender'=>'kids', 'title'=>'ccc'),
array('model'=>'678', 'brand'=>'', 'title'=>'ccc')
);
我想去:
$result = array(
array('model'=>'123', 'garment'=>'coat', 'gender'=>'men', 'brand'=>'adidas', 'title'=>'aaa'),
array('model'=>'345', 'garment'=>'jacket', 'gender'=>'kids', 'brand'=>'nike', 'title'=>'bbb'),
array('model'=>'678', 'garment'=>'trainers', 'gender'=>'kids', 'brand'=>'', 'title'=>'ccc')
)
请你帮帮我-谢谢。这是我尝试的解决方案,但是在尝试获取大型数组时内存不足。
这是第1步:
//---------------------- STEP 1 ------
$result = array();
foreach($arr as $line) {
$result[] = $line['model'];
}
$result = array_values(array_unique($result));
这是第2步:
//---------------------- STEP 2 ------
foreach($result as $r) {
$inter = array();
$inter['model'] = $r;
foreach($arr as $line) {
if ($r == $line['model']){
if (!isset($inter['title']) && isset($line['title'])) {
$inter['title'] = $line['title'];
}
if (!isset($inter['garment']) && isset($line['garment'])) {
$inter['garment'] = $line['garment'];
}
if (!isset($inter['gender']) && isset($line['gender'])) {
$inter['gender'] = $line['gender'];
}
if (!isset($inter['brand']) && isset($line['brand'])) {
$inter['brand'] = $line['brand'];
}
}
}
if (!isset($inter['title'])) {
$inter['title'] = '';
}
if (!isset($inter['garment'])) {
$inter['garment'] = '';
}
if (!isset($inter['gender'])) {
$inter['gender'] = '';
}
if (!isset($inter['brand'])) {
$inter['brand'] = '';
}
$results[] = $inter;
unset($inter);
}
unset($result);
return $results;