like the function usort in php but if two members compare as equal, their key should be same. for example: $arrayToSort = array(2,6,1,3,3);
after sort return
array
1 => 1
2 => 2
3 => 3
3 => 3
4 => 6
like the function usort in php but if two members compare as equal, their key should be same. for example: $arrayToSort = array(2,6,1,3,3);
after sort return
array
1 => 1
2 => 2
3 => 3
3 => 3
4 => 6
Not sure if there's a native function but this might be what you want.
<?php
$arr = array(1,2,2,2,3);
function arrayKeyJoin( $arr ) {
$newArr = array();
foreach ( $arr as $item ) {
if ( !in_array( $item, array_keys($newArr) ) ) {
$newArr[$item] = array();
}
array_push( $newArr[$item], $item );
}
return $newArr;
}
echo '<pre>', var_dump( arrayKeyJoin( $arr ) ), '</pre>';
Output:
array(3) {
[1]=>
array(1) {
[0]=>
int(1)
}
[2]=>
array(3) {
[0]=>
int(2)
[1]=>
int(2)
[2]=>
int(2)
}
[3]=>
array(1) {
[0]=>
int(3)
}
}