ramsey · GitHub

Why "index_key" can not be an array?
It can be useful to build tree indexes.

Examples:

$records = array(
    array( 'id' => 1 , 'parent-id' => 7 , 'name' => 'Doe'),
    array( 'id' => 2 , 'parent-id' => 3 , 'name' => 'Smith'),
    array( 'id' => 4 , 'parent-id' => 7 , 'name' => 'Jones'),
);
array_column( $records , 'name' , array( 'parent-id' , 'id' ) )
// => array( 7 => array( 1 => 'Doe' , 4 => 'Jones' ), 3 => array( 2 => 'Smith' ) )

Result is collections of childs by 'parent-id' key where 'id' is subkey and 'name' is value.

More examples: #56 (comment)

P.S. It also compatible with current array_column and proposed higher

$records = array(
    array( 'id' => 1 , 'parent-id' => 7 , 'name' => 'Doe' , 'sex' => 'male' ),
    array( 'id' => 2 , 'parent-id' => 3 , 'name' => 'Smith' , 'sex' => 'female' ),
    array( 'id' => 4 , 'parent-id' => 7 , 'name' => 'Jones' , 'sex' => 'male' ),
);
array_column( $records , array( 'name' , 'sex' ) , array( 'parent-id' , 'id' ) )
/* =>
array(
    7 => array(
        1 => array( 'Doe' , 'male' ) ,
        4 => array( 'Jones' , 'male' )
    ),
    3 => array(
        2 => array( 'Smith' , 'female' )
    )
)
*/
For myself, I call this array_collect

Read the original on github.com ↗