Prioritizing the most important value in an array I have an array of names where I need to find the most important person.

This is my array. I cannot edit this array unless I create a copy of it

$data = [
    'Jack',
    'Josh',
    'Boris',
    'Bob',
    'Boris',
    'Jack',
    'Josh'
];


The most important person is number 1. After number 1 the most improtant person is number 2 and so on. These are the priorities.


Jack 
Bob
Boris
Josh


The best way of tackling this issue I could think of is like the following:

$i = 5;
foreach ($data as $val) {
    if ($val == 'Jack' &amp;&amp; $i &gt; 1) {
        $myValue = $val;
        $i = 1;
        break;
    } else if ($val == 'Bob' &amp;&amp; $i &gt; 2) {
        $myValue = $val;
        $i = 2;
    } else if ($val == 'Boris' &amp;&amp; $i &gt; 3) {
        $myValue = $val;
        $i = 3;
    } else if ($val == 'Josh' &amp;&amp; $i &gt; 4) {
        $myValue = $val;
        $i = 4;
    }
}


It works great. But it just doesn't feel right.

Is prioritizing it like this common practice?
In most circumstances it is better to do sorting in your SQL layer

&lt;?php
$sorted_data = $data;
$importance = array_flip(['Jack', 'Bob', 'Boris', 'Josh']);
usort($sorted_data, function ($a, $b) use ($importance) {
  return $importance[$a] &lt;=&gt; $importance[$b];
});
$important_person = reset($sorted_data);


If you need to reuse your code then I suggest: 

&lt;?php
function sort_users(array $importance) : Closure {
  return function (string $user_a, string $user_b) use ($importance) : int {
      $a_priority = $importance[$user_a] ?? 0;
      $b_priority = $importance[$user_b] ?? 0;
      return $a_priority &lt;=&gt; $b_priority;
  };
}

$sorted_data = $data;
$importance = array_flip(['Jack', 'Bob', 'Boris', 'Josh']);
usort($sorted_data, sort_users($importance));
$important_person = reset($sorted_data);


Some of the above code can be removed if you can sort the original array, and if $data always contains elements of $importance 

I would suggest thinking of a better variable name for $data because it's not obvious what data is, is $first_names better?