如何对多维数组中的结果进行分组

时间:2014-02-05 18:27:57

标签: php arrays sorting multidimensional-array

我有以下代码:

    <?
$proplist=array(
array(name=>"Store #1", address=>"123 Example St",      city=>"Smyrna",         state=>"GA",    zip=>"11111"),
array(name=>"Store #2", address=>"666 Anywhere Rd",     city=>"Bristol",        state=>"VA",    zip=>"33333"),
array(name=>"Store #3", address=>"123 Any Street",      city=>"Bristol",        state=>"NC",    zip=>"44444"), 
array(name=>"Store #4", address=>"111 Someplace Rd",    city=>"Atlanta",        state=>"GA",    zip=>"22222"),
);
foreach($proplist as $prop) { 
echo "{$prop["name"]} - {$prop["address"]}, {$prop["state"]} {$prop["zipcode"]}<br>" ; 
}  
?>

哪位给我这个:

商店#1 - 123示例St,GA商店#2 - 666 Anywhere Rd,VA商店#3 - 123任何街道,NC商店#4 - 111 Someplace Rd, GA

我想要完成的是:

GA

商店#1 - 123示例St,GA
商店#4 - 111 Someplace Rd,GA

VA

Store#2 - 666 Anywhere Rd,VA

NC

Store#3 - 123 Any Street,NC

2 个答案:

答案 0 :(得分:1)

我希望这会有所帮助

public function array_group_by($array, $key) {

    if (is_null($key)) return $array;
    $result = array();

    foreach ($array as $item) {
        $group_key = $item[$key];
        if (!array_key_exists($group_key, $result)) {
            $result[$group_key] = array();
        }
        $result[$group_key][] = $item;
    }

    return $result;
}

所以你可以调用array_group_by($proplist, 'state');,它可以作为数组提供所需的结果。

var_dump(array_group_by($proplist,'state');

应输出

array (size=3)
  'GA' => 
    array (size=2)
      0 => 
        array (size=5)
          'name' => string 'Store #1' (length=8)
          'address' => string '123 Example St' (length=14)
          'city' => string 'Smyrna' (length=6)
          'state' => string 'GA' (length=2)
          'zip' => string '11111' (length=5)
      1 => 
        array (size=5)
          'name' => string 'Store #4' (length=8)
          'address' => string '111 Someplace Rd' (length=16)
          'city' => string 'Atlanta' (length=7)
          'state' => string 'GA' (length=2)
          'zip' => string '22222' (length=5)
  'VA' => 
    array (size=1)
      0 => 
        array (size=5)
          'name' => string 'Store #2' (length=8)
          'address' => string '666 Anywhere Rd' (length=15)
          'city' => string 'Bristol' (length=7)
          'state' => string 'VA' (length=2)
          'zip' => string '33333' (length=5)
  'NC' => 
    array (size=1)
      0 => 
        array (size=5)
          'name' => string 'Store #3' (length=8)
          'address' => string '123 Any Street' (length=14)
          'city' => string 'Bristol' (length=7)
          'state' => string 'NC' (length=2)
          'zip' => string '44444' (length=5)

您还可以在此处找到一个有效的示例https://eval.in/98276

答案 1 :(得分:0)

据我所知,没有内置分组功能来存档。所以唯一的方法是手动完成,如下所示:

$result = array();
foreach ($proplist as $data) {
  $id = $data['state'];
  if (isset($result[$id])) {
     $result[$id][] = $data;
  } else {
     $result[$id] = array($data);
  }
}

var_dump($result);

或者,如果您只想对这些元素进行排序:

foreach ($proplist as $key => $row) {
    $states[$key]  = $row['state']; 
}
array_multisort($states, SORT_ASC, $proplist);