鉴于我的数组
array [0]
fielda: cca
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [1]
fielda: cca
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [2]
fielda: cca
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [3]
fielda: ccb
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [4]
fielda: ccb
fieldb: my value b
fieldc: my value c
fieldd: my value d
有没有一种简单的方法来重组它,以便 fielda 像这样分组
array [0]
fielda: cca
array [0]
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [1]
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [2]
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [3]
array [1]
fielda: ccb
array [0]
fieldb: my value b
fieldc: my value c
fieldd: my value d
array [1]
fieldb: my value b
fieldc: my value c
fieldd: my value d
也许有一个更简单的方法,但这应该适合你:
$new_array = array();
foreach ($array as $values) // $array is your original array
{
$new_array[array_shift($values)][] = $values;
}
print_r($new_array);
这样的东西应该有效:
$restructured = array();
foreach( $myarray as $value ){
$key = $value['fielda'];
unset($value['fielda']);
$restructured[$key][] = $value;
}