0
 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

4 回答 4

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]);
        }
    }
}
于 2013-08-02T05:23:28.883 回答
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',
        ),
    );
于 2013-08-02T05:25:47.010 回答
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)。

于 2013-08-02T05:29:34.603 回答
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
        )

)
于 2013-08-02T05:29:57.230 回答