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
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 に答える
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 に答える