从基于公共ID的多维数组中获取多个数组

I have one large array and I want to group this into other arrays based on a common ID so that I can use array_splice to get only the first and last occurrence of that ID.

array( 
    [0] => array(id => 34, name = "walter"),
    [1] => array(id => 25, name = "walter jr"),
    [2] => array(id => 34, name = "saul"),
    [3] => array(id => 25, name = "jesse"),
    [4] => array(id => 25, name = "todd")
   )

What I want to end up with is something like this.

array( 
    [0] => array(
                 id => 34, name = "walter",
                 id => 34, name = "saul"
                ),

    [1] => array(
                 id => 25, name = "walter jr",
                 id => 25, name = "jesse",
                 id => 25, name = "todd"
                 )
     )

I'm having a really hard time trying to wrap my head around how to accomplish this and have searched all over. I've found some solutions using array_unique and array_diff but i'm never able to get the result i'm looking for.

You can use array_reduce to group array elements, see below:

$data = array(
  0 => array('id' => 34, 'name' => "walter"),
  1 => array('id' => 25, 'name' => "walter jr"),
  2 => array('id' => 34, 'name' => "saul"),
  3 => array('id' => 25, 'name' => "jesse"),
  4 => array('id' => 25, 'name' => "todd")
);

$result = array_reduce($data, function ($result, $item){
  if (!isset($result[$item['id']])) {
        $result[$item['id']] = array();
  }
  $result[$item['id']][] = $item;
  return $result;
}, array());
print_r(array_values($result));

and result is:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 34
                    [name] => walter
                )

            [1] => Array
                (
                    [id] => 34
                    [name] => saul
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 25
                    [name] => walter jr
                )

            [1] => Array
                (
                    [id] => 25
                    [name] => jesse
                )

            [2] => Array
                (
                    [id] => 25
                    [name] => todd
                )

        )

)

If you want the key 0,1,2,3,4 ... just go through the entire array and overwrite the keys. But depending on what you are doing, use foreach to traverse the array without the key. =p

$all_array = array(
    0 => array( 'id' => 34 , 'name' => "walter" ) ,
    1 => array( 'id' => 25 , 'name' => "walter jr" ) ,
    2 => array( 'id' => 34 , 'name' => "saul" ) ,
    3 => array( 'id' => 25 , 'name' => "jesse" ) ,
    4 => array( 'id' => 25 , 'name' => "todd" )
) ;


$array_sort = array() ;

foreach ( $all_array as $piece_array ) {
    $array_sort[$piece_array['id']][] = $piece_array ;
}

ksort( $array_sort ) ;

var_dump( $array_sort ) ;