Derrand
Derrand

Reputation: 3

Sort Based On Array Of Priority

I have an algorithm that users can manually enter and it will display the header and footer. Although in my function there is a priority field, and I want everything to be sorted with priority.

3 Parameters - Field, Priority, Type Keep in mind I already have a array of head/foot with priority preset, this function is to add additional or overwrite the priority. It then gets stored in another array to simplify. I then want to sort it all with the priority.

Array looks like this:

  'js-amcharts-export' =>  array('link' => 'assets/global/plugins/amcharts/amcharts/plugins/export/export.min.js',
                                    'type' => 'text/javascript',
                                    'default' => false,
                                    'priority' => ''),

Example:

$script->require_meta('js-amcharts-export', '20')

This would make sure all the other ones with priority 10 lets say get loaded first then the 20, and everything after.

Basically I need to sort the array based on 'priority'

Would asort($header_array['priority'] work?

Upvotes: 0

Views: 172

Answers (2)

mtr.web
mtr.web

Reputation: 1515

In your case, the code would look like this:

function cmp($a, $b) {
    if ($a['priority'] == $b['priority']) {
        return 0;
    }
    return ($a['priority'] < $b['priority']) ? -1 : 1;
}
uasort($header_array, 'cmp');

Upvotes: 0

user3720435
user3720435

Reputation: 1476

I think the PHP array sort you are looking for uasort. http://php.net/manual/en/function.uasort.php

Upvotes: 1

Related Questions