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

Php 如何对多维数组的结果进行分组,php,arrays,sorting,multidimensional-array,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",

我有以下代码:

    <?
$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示例街,佐治亚州
商店#2-666任意路,弗吉尼亚州
商店#3-123任意街,北卡罗来纳州
商店#4-111某地路,佐治亚州

我想要实现的是:

GA 商店#1-123例如佐治亚州圣路易斯街
佐治亚州某地路4-111号商店

弗吉尼亚州 弗吉尼亚州Anywhere路2-666号商店

数控 商店#3-123北卡罗来纳州任何一条街,我希望这能有所帮助

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;
}
因此,您可以通过($proplist,'state')调用
array\u group\u它将作为数组提供所需的结果

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)

您还可以在这里找到一个工作示例

据我所知,没有用于存档此文件的分组内置函数。因此,唯一的方法是手动执行,如下所示:

$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);