0

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?

Derrand
  • 3
  • 2
  • 1
    Possible duplicate of [Sort Multi-dimensional Array by Value](https://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – ficuscr Sep 22 '17 at 16:26

2 Answers2

1

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

user3720435
  • 1,421
  • 1
  • 17
  • 27
0

+1 to @user3720435 for the answer.

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');
mtr.web
  • 1,505
  • 1
  • 13
  • 19