PHP数组按键展平多维数组

时间:2013-09-08 11:37:13

标签: php multidimensional-array flatten

这是我的初始数组:

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

3 个答案:

答案 0 :(得分: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行,如果你的循环内存不足,很明显它会出现逻辑错误 另外,我认为你不能在数组中保留相同的字符串键名。

答案 1 :(得分: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>';
?>

答案 2 :(得分:0)

我的方法假定输入子阵列分批为3 array_replace()中的第一个空关联数组用于建立OP所需的密钥顺序 我正在使用splat运算符...,因此需要php5.6 + splat操作符一次在块中传递所有三个子数组。

代码:(Demo

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