2

这是我的初始数组:

$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;
4

3 回答 3

1
$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')
);
//var_dump($arr);   
$arrRes = array();
$arrSort = array();

foreach($arr as $key => $v){

$key = array_search($v["model"],$arrSort,true);
if($key === false){
    //key empty , insert model 
    $arrRes[] = $v;
    $arrSort[] = $v["model"];
    }
else{

    foreach($v as $subKey => $subv){
        if($subKey!="model"){       
            $arrRes[$key][$subKey] = $subv;
        }
    }   
}   

}
echo "<pre>";   
var_dump($arrRes);  
echo "</pre>";

我尝试了大约 8000 行,如果你的循环内存不足,显然它也有逻辑错误,我认为你不能在数组中保留相同的字符串键名。

于 2013-09-08T12:50:18.710 回答
0
<?php

$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')
);

$new_arr = array();

foreach ($arr AS $tmp) {
  $this_model = $tmp['model'];
  if (!isset($new_arr[$this_model])) { $new_arr[$this_model] = array(); }
  $new_arr[$this_model] = array_merge($new_arr[$this_model], $tmp);
}
print '<pre>';
var_dump($new_arr);
print '</pre>';
?>
于 2013-09-08T11:51:43.893 回答
0

我的方法假设输入子数组以 3个为一组。
第一个空关联数组array_replace()用于建立 OP 所需的键顺序。
我正在使用 splat 运算符...,因此需要 php5.6+。
splat 运算符一次提供块中的所有三个子数组。

代码:(演示

$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')  
);
$key_order=['model'=>'','garment'=>'','gender'=>'','brand'=>'','title'=>''];
foreach(array_chunk($arr,3) as $a){
    $result[]=array_replace($key_order,...$a);
}
var_export($result);

输出:

array (
  0 => 
  array (
    'model' => '123',
    'garment' => 'coat',
    'gender' => 'men',
    'brand' => 'adidas',
    'title' => 'aaa',
  ),
  1 => 
  array (
    'model' => '345',
    'garment' => 'jacket',
    'gender' => 'kids',
    'brand' => 'nike',
    'title' => 'bbb',
  ),
  2 => 
  array (
    'model' => '678',
    'garment' => 'trainers',
    'gender' => 'kids',
    'brand' => '',
    'title' => 'ccc',
  ),
)
于 2017-07-31T13:17:19.637 回答