Try this code.
$arr = array(
'Animal Dog3',
'Animal Dog1',
'Animal Dog2',
'Subject Physics',
'Place1 Tokyo',
'Place4 Tokyo',
'Sport Tennis'
);
$order_array = array('tokyo', 'tennis', 'dog');
$sort_function = function($a, $b) use($order_array)
{
$return = strcasecmp($a, $b);
foreach ($order_array as $word)
{
// if each string contains `tokyo` -- alphabetical order
if (stripos($a, $word) !== false && stripos($b, $word) !== false)
{
$return = strcasecmp($a, $b);
break;
}
// if $a string contains `tokyo` -- $a goes first
elseif (stripos($a, $word) !== false)
{
$return = -1;
break;
}
// if $b string contains `tokyo` -- $b goes first
elseif (stripos($b, $word) !== false)
{
$return = 1;
break;
}
// if $a and $b does not contains -- lets take `tennis`
else
{
continue; // just for readablity
}
}
return $return;
};
usort($arr, $sort_function);
var_dump($arr);
// ["Place1 Tokyo","Place4 Tokyo","Sport Tennis","Animal Dog1","Animal Dog2","Animal Dog3","Subject Physics"]
Or this one
$arr = array(
'Animal Dog3',
'Animal Dog1',
'Animal Dog2',
'Subject Physics',
'Place1 Tokyo',
'Place4 Tokyo',
'Sport Tennis'
);
$order_array = array('tokyo', 'tennis', 'dog');
$sort_function = function($a, $b) use($order_array)
{
$a_index = sizeof($order_array); // lets suppose that it's last
$b_index = sizeof($order_array); // lets suppose that it's last
$i = 0;
foreach ($order_array as $word)
{
if (stripos($a, $word) !== false)
$a_index = $i; // remeber index order of $a
if (stripos($b, $word) !== false)
$b_index = $i; // remeber index order of $b
$i++;
}
if ($a_index == $b_index) // if indexes are equal
return strcasecmp($a, $b); // alphabetical order
else
return $a_index - $b_index; // index order
};
usort($arr, $sort_function);
var_dump($arr);
// ["Place1 Tokyo","Place4 Tokyo","Sport Tennis","Animal Dog1","Animal Dog2","Animal Dog3","Subject Physics"]
["tokyo", "tennis", "dog"]What should be the result of"tokyo dog" < "tennis"then?