如果有相同的密钥,则循环并合并

时间:2013-08-02 04:10:47

标签: php multidimensional-array

 Array ( [0] => Array ( [field_airline] => 18 [title] => FGGH [field_route_location] => Hongkong [field_time] => 01:10 ) 
         [1] => Array ( [field_airline] => 19 [title] => DSSA [field_route_location] => Kuala Lumpur [field_time] => 01:10 ),
         [2] => Array ( [field_airline] => 19 [title] => ASAS [field_route_location] => Kuala Lumpur [field_time] => 01:10 )
)

我想要的合并结果

 Array ( [0] => Array ( [field_airline] => 18 [title] => FGGH [field_route_location] => Hongkong [field_time] => 01:10 ), 
         [1] => Array ( [field_airline] => 19 [title] => array(DSSA,ASAS) [field_route_location] => Kuala Lumpur [field_time] => 01:10)
)

我正在尝试

<?php 

$a= Array ( 0 => Array ( 'field_airline' => 18 ,
                        'title' => 'FGGH',
                         'field_route_location' => 'Hongkong',
                        'field_time' => '01:10' 
                ), 
          1 => Array ( 'field_airline' => 19 ,
                        'title' => 'DSSA',
                        'field_route_location' => 'Kuala Lumpur',
                        'field_time' => '01:10'
                        ),
          2 => Array ( 'field_airline' => 19,
                       'title' => 'ASAS',
                       'field_route_location' => 'Kuala Lumpur',
                       'field_time' => '01:10'
                    )
    );
$b = array();
foreach ($a as $key=>$value){
    $b[$value['field_route_location']][] = $value;
}
echo '<pre>';
print_r($b);
echo '</pre>';
?>

4 个答案:

答案 0 :(得分:1)

foreach ($a as $ka=>&$va)
{
    foreach ($a as $kb=>$vb)
    {
        if ($va['field_airline'] == $vb['field_airline'] && $ka != $kb)
        {
            if (is_array($va['title']))
            {
                $va['title'][] = $vb['title'];
            }
            else
            {
                $va['title'] = array($va['title'], $vb['title']);
            }
            // repeat if/else with the other fields if needed
            unset($a[$kb]);
        }
    }
}

答案 1 :(得分:1)

<强>概念

创建一个空白结果数组。循环遍历给定的数据。对于每个项目,如果结果数组中不存在,请按下它。如果确实存在,请将项目的title推送到该field_airline元素的结果数组。

<强>代码:

$array_result = array();

if ( ! empty($array)){
    foreach($array as $item){
        // item already exists in results
        if (isset($array_result[$item['field_airline']])){
            // prepare title
            $title = $array_result[$item['field_airline']]['title'];
            $title[] = $item['title']; // push into existing array
            // replace array of titles
            $array_result[$item['field_airline']]['title'] = $title;
        }
        // encountering for first time
        else{
            $array_result[$item['field_airline']] = array(
                'field_airline' => $item['field_airline'],
                'title' => array($item['title']),
                'field_route_location' => $item['field_route_location'],
                'field_time' => $item['field_time'],
            );          
        }
    }
}

$array_result = array_values($array_result); // reset the keys in results

测试代码:

$array = array(
        array(
            'field_airline' => 18,
            'title' => 'FGGH',
            'field_route_location' => 'Hongkong',
            'field_time' => '01:10',
        ),
        array(
            'field_airline' => 19,
            'title' => 'DSSA',
            'field_route_location' => 'Kuala Lumpur',
            'field_time' => '01:10',
        ),
        array(
            'field_airline' => 19,
            'title' => 'ASAS',
            'field_route_location' => 'Kuala Lumpur',
            'field_time' => '01:10',
        ),
    );

答案 2 :(得分:1)

试试这个。

<?php
$arr=array('0' => array('field_airline' => 18,
                                 'title' =>  'FGGH',
                                'field_route_location' => 'Hongkong',
                                'field_time' => '01:10'),

                    '1' => array('field_airline' => 19,
                                 'title' =>  'DSSA',
                                'field_route_location' => 'Kuala Lumpur',
                                'field_time' => '01:10'),

                    '2' => array('field_airline' => 19,
                                 'title' =>  'ASAS',
                                'field_route_location' => 'Test',
                                'field_time' => '01:10')
);


$final=array();

$uniqkeys=array(); // to check for unique field_airline
foreach($arr as $key => $subarr)
{
  if(!in_array($subarr['field_airline'],$uniqkeys))
  { 
   $uniqkeys[]=$subarr['field_airline'];
   $final[$subarr['field_airline']]= $subarr;
  }
  else
  {
    // Check if title is not same, add title to array
     if($final[$subarr['field_airline']]['title'] != $subarr['title'])
    {
         $final[$subarr['field_airline']]['title']=array(
         $final[$subarr['field_airline']]['title'], $subarr['title']);
    }
    //Check for location
    if($final[$subarr['field_airline']]['field_route_location'] != $subarr['field_route_location'])
    {
         $final[$subarr['field_airline']]['field_route_location']=array(
         $final[$subarr['field_airline']]['field_route_location'], $subarr['field_route_location']);
    }

 // Check for filed time
    if($final[$subarr['field_airline']]['field_time'] != $subarr['field_time'])
    {
         $final[$subarr['field_airline']]['field_time']=array(
         $final[$subarr['field_airline']]['field_time'], $subarr['field_time']);
    }

  }
}

print_r($final);

?>

点击此处的工作示例(http://codepad.org/UkOZq9gy)。

答案 3 :(得分:1)

function sortByLocation($a, $b)
{
    return strcmp($a["field_route_location"], $b["field_route_location"]);
}

$a = Array(
    0 => Array('field_airline' => 18,
        'title' => 'FGGH',
        'field_route_location' => 'Hongkong',
        'field_time' => '01:10'
    ),
    1 => Array('field_airline' => 19,
        'title' => 'DSSA',
        'field_route_location' => 'Kuala Lumpur',
        'field_time' => '01:10'
    ),
    2 => Array('field_airline' => 19,
        'title' => 'ASAS',
        'field_route_location' => 'Kuala Lumpur',
        'field_time' => '01:10'
    )
);

usort($a, 'sortByLocation'); // sorting array by location

$b = array();

foreach ($a as $key => $value)
{
    $title = array();

    if (!isset($b[$value['field_route_location']]))
        $b[$value['field_route_location']] = $value;
    else
    {
        if (!is_array($b[$value['field_route_location']]['title']))
            array_push($title, $b[$value['field_route_location']]['title']);
        else
            $title = $b[$value['field_route_location']]['title'];

        array_push($title, $a[$key]['title']);
        $b[$value['field_route_location']]['title'] = $title;
    }
}
echo '<pre>';
print_r($b);
echo '</pre>';

输出

Array
(
    [Hongkong] => Array
        (
            [field_airline] => 18
            [title] => FGGH
            [field_route_location] => Hongkong
            [field_time] => 01:10
        )

    [Kuala Lumpur] => Array
        (
            [field_airline] => 19
            [title] => Array
                (
                    [0] => ASAS
                    [1] => DSSA
                )

            [field_route_location] => Kuala Lumpur
            [field_time] => 01:10
        )

)