PHP - 如何有条件地合并数组中的数组的某些键

时间:2018-04-13 11:26:39

标签: php arrays loops

我有一个数组数组。我需要修改它,只有当数组的companytype与其任何兄弟数组相同时,才会合并它们的cars数组。请考虑以下数组:

array(
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Compass',
        ), 
    ),
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Wrangler',
        ), 
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'Sedan',
        'cars'     => array(
            'C-Class',
        ),
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'SUV',
        'cars'     => array(
            'G Wagon',
        ),
    ),
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Cherokee',
        ), 
    ),
)

采用上述数组后的预期输出应为:

array(
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Compass',
            'Wrangler',
            'Cherokee',
        ), 
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'Sedan',
        'car'     => array(
            'C-Class',
        ),
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'SUV',
        'car'     => array(
            'G Wagon',
        ),
    ),
)

我尝试了foreach循环,但我无法弄明白。我认为可以通过递归完成,但我不应该从哪里开始。

    foreach ( $array as $one ) {
        foreach ( $array as $two ) {
            $zzz = [];
            if ( $one['company'] === $two['company'] && $one['type'] === $two['type'] ) {
                if ( $one['cars'] === $two['cars'] ) {
                    $zzz = $one;
                } else {
                    $zzz = [
                        'company'    => $one['company'],
                        'type' => $one['type'],
                        'cars'     => array_merge( $one['cars'], $two['cars'] ),
                    ];
                }
            } else {
                $zzz = $one;
            }
            $temp[] = $zzz;
        }
    }

2 个答案:

答案 0 :(得分:1)

这是一个看起来很困难的简单事情之一:

$input = array( /* ... your original array ... */ );
$output = array();

foreach ($input as $icar)
{
    $filed = false;
    foreach ($output as $okey => $ocar) // look for existing brand and type to file into
        if ($ocar['company'] == $icar['company'] && $ocar['type'] == $icar['type'])
        {
            $output[$okey]['cars'][] = $icar['cars'][0];
            $filed = true;
            break; // no need to continue once we put it somewhere
        }

    if (!$filed) // it's a new brand and type so we copy the entire thing
        $output[] = $icar;
}

print_r($output);

答案 1 :(得分:0)

您需要通过循环遍历数组并检查条件是否已设置公司和类型

$arr  =array(
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Compass',
        ), 
    ),
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Wrangler',
        ), 
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'Sedan',
        'cars'     => array(
            'C-Class',
        ),
    ),
    array(
        'company' => 'Mercedes',
        'type'    => 'SUV',
        'cars'     => array(
            'G Wagon',
        ),
    ),
    array(
        'company' => 'Jeep',
        'type'    => 'SUV',
        'cars'    => array(
            'Cherokee',
        ), 
    ),
);
$new_array = array();
foreach ($arr as  $value) {
  if(isset($new_array[$value['company']."_".$value['type']]))
    $new_array[$value['company']."_".$value['type']]['cars'][]=$value['cars'][0];
  else
    $new_array[$value['company']."_".$value['type']]=$value;  
}
$new_array = array_values($new_array);
var_dump($new_array);