PHP中的组数组相同的键

时间:2016-12-06 04:44:08

标签: php

我有这样的数组数据: -

  Array
    (
        [0] => Array
            (
                [lane_id] => 1
                [lane_name] => Jagorawi (arah ke Bogor & Ciawi)
                [lat] => -6.267049789428711
                [lng] => 106.873229980468750
            )

        [1] => Array
            (
                [lane_id] => 1
                [lane_name] => Jagorawi (arah ke Bogor & Ciawi)
                [lat] => -6.267449855804443
                [lng] => 106.873207092285160
            )

        [2] => Array
            (
                [lane_id] => 1
                [lane_name] => Jagorawi (arah ke Bogor & Ciawi)
                [lat] => -6.267680168151855
                [lng] => 106.873199462890620
            )
)

我想通过line_id对数据组进行分组,而line_id有更多的latlang。

2 个答案:

答案 0 :(得分:0)

新数组:

<?php

$original_array = array(
        array(
                "lane_id" => 1,
                "lane_name" => 'Jagorawi (arah ke Bogor & Ciawi)',
                "lat" => -6.267049789428711,
                "lng" => 106.873229980468750
            ),
        array(
                "lane_id" => 1,
                "lane_name" => 'Jagorawi (arah ke Bogor & Ciawi)',
                "lat" => -6.267449855804443,
                "lng" => 106.873207092285160
            ),
        array(
                "lane_id" => 1,
                "lane_name" => 'Jagorawi (arah ke Bogor & Ciawi)',
                "lat" => -6.267680168151855,
                "lng" => 106.873199462890620
            )
);    

$arr_lanes = array();

foreach( $original_array as $arr_lane => $arr)
{
    foreach($arr as $key => $value)
    {
        //echo $key . ": ". $value."<br>";

        if($key!="lat" && $key!="lng")
        {
            $arr_lanes[$key] = $arr[$key];
        }
        else
        {
            if($key=='lat') $arr_lanes["lats"][]= $arr[$key];
            else
            if($key=='lng') $arr_lanes["lngs"][]= $arr[$key];    
        }
    }
}

var_dump($arr_lanes );

/*

Output:

array (size=4)
  'lane_id' => int 1
  'lane_name' => string 'Jagorawi (arah ke Bogor & Ciawi)' (length=32)
  'lats' => 
    array (size=3)
      0 => float -6.2670497894287
      1 => float -6.2674498558044
      2 => float -6.2676801681519
  'lngs' => 
    array (size=3)
      0 => float 106.87322998047
      1 => float 106.87320709229
      2 => float 106.87319946289

*/      

答案 1 :(得分:0)

//fill $origAry
$origAry = [];
array_push($origAry, ['lane_id' => 1, 'lane_name' => 'lane1', 'more_data' => 'first']);
array_push($origAry, ['lane_id' => 1, 'lane_name' => 'lane1', 'more_data' => 'second']);
array_push($origAry, ['lane_id' => 2, 'lane_name' => 'lane2', 'more_data' => 'third']);
array_push($origAry, ['lane_id' => 1, 'lane_name' => 'lane1', 'more_data' => 'fourth']);
array_push($origAry, ['lane_id' => 2, 'lane_name' => 'lane2', 'more_data' => 'fifth']);

//fill $dataByLaneId such that $dataByLaneId[$laneId] is a subset
// of $origAray where lane_id == $laneId
$dataByLaneId = [];
foreach ($origAry as $val) {
    if (!isset($dataByLaneId[$val['lane_id']])) {
        $dataByLaneId[$val['lane_id']] = [];
    }
    array_push($dataByLaneId[$val['lane_id']], $val);
}

print_r($dataByLaneId);

输出:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [lane_id] => 1
                    [lane_name] => lane1
                    [more_data] => first
                )

            [1] => Array
                (
                    [lane_id] => 1
                    [lane_name] => lane1
                    [more_data] => second
                )

            [2] => Array
                (
                    [lane_id] => 1
                    [lane_name] => lane1
                    [more_data] => fourth
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [lane_id] => 2
                    [lane_name] => lane2
                    [more_data] => third
                )

            [1] => Array
                (
                    [lane_id] => 2
                    [lane_name] => lane2
                    [more_data] => fifth
                )

        )

)

PHP Sandbox Example