0

Like the title sais I have no idea where to start and using asort() and sort() are not helping in the way I thought it would. Essentially I have an array as such:

$array = array(
    'array_c' => array(
        'array_b' => (
            array('object' => 'e some Object'),
            array('object' => 'b some Object'),
        ),
        'array_a' => (
            array('object' => 'awesome Object'),
        ),
    ),
    'array_a' => array(
        'array_e' => (
            array('object' => 'e some Object'),
        ),
        'array_a' => (
            array('object' => 'b awesome Object'),
        );
    );
);

So I was looking at asort as I want to keep the associations the same, The function I have started writing is:

function sort_me(some_array){
    $new_array = asort(some_array);
    return $new_array;
}

this function then takes in $array['array_c'] so that you get back an alphabetically sorted array that looks like:

    'array_c' => array(
        'array_a' => (
            array('object' => 'awesome Object'),
        ),
        'array_b' => (
            array('object' => 'b some Object'),
            array('object' => 'e some Object'),
        ),

    ),

can some one tell me why my function is not working? Am I misunderstanding the power of asort?

3
  • You want ksort() if you want to sort based on the string keys ;-) Commented Jan 2, 2013 at 16:43
  • really? Hmm ill look into kSort(). I don't think so, according to this it will sort the the key, if you look at my end result the value is alphabetical. I pass in the key to sort its value. Also k sort does nothing as the array remains unsorted. Commented Jan 2, 2013 at 16:43
  • This minimal reproducible example is not super clear. Commented Jan 23, 2024 at 7:44

1 Answer 1

1

ksort is the way to go, but ksort does not return a newly sorted array:

http://us.php.net/ksort

it returns a bool -> true if the array could be sorted, otherwise false...

this code snipped should do what you need:

ksort($array);
foreach($array as $key=>$value){
    ksort(value);
    $array[$key]=$value;
}
print_r($array);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.