PHP:在多维数组中如何合并具有相同键值的子数组?

时间:2015-03-23 07:47:05

标签: php arrays sorting multidimensional-array

我有数组多维数组,

Array(

    Array
    (
        [UTTL_M_FT] => 2616.00
        [TUG] => 5310
        [univercity] => 1
        [year] => 2010
    )

    Array
    (
        [UTTL_M_FT] => 2710.00
        [TUG] => 5405
        [univercity] => 1
        [year] => 2012
    )

    Array
    (
        [UTTL_M_FT] => 2622.00
        [TUG] => 7803
        [univercity] => 2
        [year] => 2010
    )

    Array
    (
        [UTTL_M_FT] => 2759.00
        [TUG] => 8423
        [univercity] => 2
        [year] => 2012
    )

    Array
    (
        [UTTL_M_FT] => 3330.00
        [TUG] => 6879
        [univercity] => 3
        [year] => 2011
    )

    Array
    (
        [UTTL_M_FT] => 3373.00
        [TUG] => 6796
        [univercity] => 3
        [year] => 2013
    )

    Array
    (
        [UFTF_ASN] => 749
        [UFTF_TTL] => 5296
        [univercity] => 1
        [year] => 2013
    )

)

我们需要以下格式的数组,

Array(

[univercity] => 1,

Array
    (
        [UTTL_M_FT] => 2616.00
        [TUG] => 5310       
        [year] => 2010
    ),

Array
    (
        [UTTL_M_FT] => 2710.00
        [TUG] => 5405
        [year] => 2012
    ),  

Array
    (
        [UFTF_ASN] => 749
        [UFTF_TTL] => 5296
        [year] => 2013
    )

),


Array(
[univercity] => 2,

Array
    (
        [UTTL_M_FT] => 2759.00
        [TUG] => 8423       
        [year] => 2012
    )

),  

Array(

[univercity] => 3,

    Array
    (
        [UTTL_M_FT] => 3330.00
        [TUG] => 6879       
        [year] => 2011
    ),
    Array
    (
        [UTTL_M_FT] => 3373.00
        [TUG] => 6796
        [year] => 2013
    )
);

2 个答案:

答案 0 :(得分:1)

这将适用于您,将相同的university id数组附加到主容器数据数组

<?php   

     $data =array();
        foreach($inpuArray as $array){
            $universityId = $array['univercity'];
            unset($array['univercity']);
            $data['univercity'][$universityId][] = $array;
        }
        print_r($data);

    ?>

答案 1 :(得分:0)

这对我有用:

$new = array();
$i=0;
foreach($array as $a){
    $u = $a['univercity'];
    unset($a['univercity']);
    $found = false;
    foreach($new as $k=>$v){
        if(isset($new[$k]['univercity']) && $new[$k]['univercity'] == $u){
            $new[$k][] = $a;
            $found = true;
            break;
        }
    }
    if(!$found){
        $new[$i]['univercity']=$u;
        $new[$i][]=$a;
        $i++;
    }
}
echo"<pre>";
var_export($new);
echo"</pre>";

输出:

array (
  0 => 
  array (
    'univercity' => 1,
    0 => 
    array (
      'UTTL_M_FT' => 2616,
      'TUG' => 5310,
      'year' => 2010,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 2710,
      'TUG' => 5405,
      'year' => 2012,
    ),
    2 => 
    array (
      'UFTF_ASN' => 749,
      'UFTF_TTL' => 5296,
      'year' => 2013,
    ),
  ),
  1 => 
  array (
    'univercity' => 2,
    0 => 
    array (
      'UTTL_M_FT' => 2622,
      'TUG' => 7803,
      'year' => 2010,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 2759,
      'TUG' => 8423,
      'year' => 2012,
    ),
  ),
  2 => 
  array (
    'univercity' => 3,
    0 => 
    array (
      'UTTL_M_FT' => 3330,
      'TUG' => 6879,
      'year' => 2011,
    ),
    1 => 
    array (
      'UTTL_M_FT' => 3373,
      'TUG' => 6796,
      'year' => 2013,
    ),
  ),
)