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?

1

2 Answers 2

1

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

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

1 Comment

This is not a generous answer. It could have been a comment under the question.
0

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');

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.