4

I would like to Sort an Array with its sub array value ("Name") but keeping its original array key.

default Array:

Array (
    [251] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [323] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [127] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

By using array_multisort, I can able to get following Array:

Array(
    [0] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [1] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [2] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

But what i need is,

Array(
    [323] => Array
        (
            [color] => 
            [name] => 7
            [nbr] => 2
            [url_name] => taille-7
            [meta_title] => 
        )

    [251] => Array
        (
            [color] => 
            [name] => 8
            [nbr] => 1
            [url_name] => taille-8
            [meta_title] => 
        )

    [127] => Array
        (
            [color] => 
            [name] => 34
            [nbr] => 2
            [url_name] => taille-34
            [meta_title] => 
        )
);

Thanks in adv :)

2
  • check usort function Commented Sep 20, 2016 at 9:10
  • please add array_multisort code Commented Sep 20, 2016 at 9:10

4 Answers 4

3

I would go with uasort, it looks much simpler to me:

// $arr is your Array
uasort($arr, function ($a, $b) {
  return $a['name'] - $b['name'];
});

Here is an example: http://sandbox.onlinephpfunctions.com/code/a9f2d1e9702834b3a35206125429739222770301

Sign up to request clarification or add additional context in comments.

Comments

1

$arr being your array:

//obtain list of values to sort by
foreach ($arr as $id => $value) {
    $names[$id] = $value['name'];
}
$keys = array_keys($arr);
array_multisort(
    $names, SORT_ASC, SORT_NUMERIC, $arr, $keys
);
$result = array_combine($keys, $arr);

You were probably missing the last step combining the array with given keys.

2 Comments

I have checked your code, Does not gives the result
@NarayanBhandari check again, please: sandbox.onlinephpfunctions.com/code/…
1

$arr assuming your array containing numeric keys and sort it by using array_multisort.

array_multisort will return sorted array.
array_combine will combine your original keys with sorted array.

Use:

$result = array_sort_by_column_preserve_keys($arr);
echo '<pre>';print_r($result);echo '</pre>';

Method :

function array_sort_by_column_preserve_keys($arr) {
    $ar2 = [];
    foreach($arr as $key => $sub) {
        $ar2[ $key ] = $sub;
    }
    $keys = array_keys($arr);
    array_multisort($ar2, SORT_ASC, SORT_NUMERIC, $arr, $keys);
    return $result = array_combine($keys, $arr);
}

1 Comment

You could have edited my answer, since it's basically the same.
0

Please try this,

$array=array(
    "251" => array(
        "color" => "",
        "name" => 8,
        "nbr" => 1,
        "url_name" => "taille-8",
        "meta_title" => ""),

    "323" => array(
        "color" => "",
        "name" => 7,
        "nbr" => 2,
        "url_name" => "taille-7",
        "meta_title" => ""),
    "127" => array(
        "color" => "",
        "name" => 34,
        "nbr" => 2,
        "url_name" => "taille-34",
        "meta_title" => ""),
    );
function swapArray( &$arr,$firstPos,$secondPos){
    //echo PHP_EOL."swap: ".$firstPos.", ".$secondPos.PHP_EOL;
    foreach($arr[$firstPos] as $k=>$v){
        //echo PHP_EOL.$k." => ".$v.PHP_EOL;
        $tmp=$v;
        $arr[$firstPos][$k]=$arr[$secondPos][$k];
        $arr[$secondPos][$k]=$tmp;
    }
}
    var_dump($array);

    $keys=array(); // store all key values
    $num=count($array);

    foreach($array as $key=>$tmpArray)
        $keys[]=$key;

    //var_dump($keys);

    for($i=0; $i<$num; $i++){
        for($j=$i+1; $j<$num; $j++){
            if($array[$keys[$i]]["name"]>$array[$keys[$j]]["name"]){
                swapArray($array,$keys[$i],$keys[$j]);
            }
        }
    }


    var_dump($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.