Sort a 2d array by its name column but ignoring the names' prefixes
<p>I want to sort an array of rows by the <code>name</code> column value, but critically I want to sort while ignoring the users' prefixes.</p>
<p>Sample array:</p>
<pre><code>$ad_users = [
    [&quot;name&quot; =&gt; &quot;Mr San&quot;, &quot;department&quot; =&gt; &quot;five&quot;],
    [&quot;name&quot; =&gt; &quot;Mr VSan&quot;, &quot;department&quot; =&gt; &quot;six&quot;],
    [&quot;name&quot; =&gt; &quot;Mr QSan&quot;, &quot;department&quot; =&gt; &quot;four&quot;],
    [&quot;name&quot; =&gt; &quot;Sr ASan&quot;, &quot;department&quot; =&gt; &quot;two&quot;],
    [&quot;name&quot; =&gt; &quot;Dr ASan&quot;, &quot;department&quot; =&gt; &quot;one&quot;],
    [&quot;name&quot; =&gt; &quot;Dr FSan&quot;, &quot;department&quot; =&gt; &quot;three&quot;]
];
</code></pre>
<p>Desired result:</p>
<pre><code>[
    [&quot;name&quot; =&gt; &quot;Dr ASan&quot;, &quot;department&quot; =&gt; &quot;one&quot;],
    [&quot;name&quot; =&gt; &quot;Sr ASan&quot;, &quot;department&quot; =&gt; &quot;two&quot;],
    [&quot;name&quot; =&gt; &quot;Dr FSan&quot;, &quot;department&quot; =&gt; &quot;three&quot;],
    [&quot;name&quot; =&gt; &quot;Mr QSan&quot;, &quot;department&quot; =&gt; &quot;four&quot;],
    [&quot;name&quot; =&gt; &quot;Mr San&quot;, &quot;department&quot; =&gt; &quot;five&quot;],
    [&quot;name&quot; =&gt; &quot;Mr VSan&quot;, &quot;department&quot; =&gt; &quot;six&quot;]
]
</code></pre>
<p>My current code:</p>
<pre><code>for ($x = 0; $x &lt; count($ad_users); $x++) {
    $ad_users[$x]['name']= ucwords($ad_users[$x]['name']);
    $end = (explode(',', $ad_users[$x]['name']));
    $lastname = array_pop($end);
    sort($end);
    $firstname = implode(&quot; &quot;, $end);
    $ad_users[$x]['name']=$lastname.&quot; &quot;.$firstname;
}
sort($ad_users);
for ($x = 0; $x &lt; count($ad_users); $x++) {
    echo $ad_users[$x]['name'].&quot;\n&quot;;
}
</code></pre>