0

I can't seem to sort my array using the standard PHP functions and I don't understand why. Here is;

array (size=5)
  0 => string 'Favourite Band' (length=14)
  1 => string 'Favourite Pizza' (length=15)
  2 => string 'Favourite Side' (length=14)
  3 => string 'Favourite Country' (length=17)
  4 => string 'Favourite Band' (length=14)

Do I have to use some complex callback on this array or am I overthinking it and missing something obvious?

sort($array), rsort($array), arsort($array) just return a boolean for some reason.

My code;

protected function getUndefinedFields($contacts)
{
    $array = [];

    foreach ($contacts as $contact) {
        foreach ($contact['sub']['customFields'] as $key => $sub_array) {
            $array[] = $sub_array['type'];
        }
    }

}

Edit - Thanks to your comments the penny has dropped. I was doing;

$array = asort($array)

Thinking that would set $array to the sorted array. Not the case. It makes sense to me now that it would return bool but didn't at the time.

2
  • 2
    sort($array);var_dump($array); should print the sorted array. Sort() - Returns TRUE on success or FALSE on failure. Commented May 13, 2016 at 9:35
  • Don't I feel silly Commented May 13, 2016 at 9:41

2 Answers 2

2

Sorting functions like these you enumerated as examples, work on references to array and modify your source array.

The return bolean value only says is sorting ended with success.

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

Comments

0

asort() succesfully sorting this array.

$arr = array('Favourite Band','Favourite Pizza','Favourite Side','Favourite Country','Favourite Band');
asort($arr);

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.