PHP 按特定顺序数组对二维字符串数组进行排序
PHP sort 2d string array by specific order array
我是 php 的新手,一直在尝试使用特定顺序数组对二维字符串数组进行排序。一直在考虑使用 'usort' 和 'comparison',但在使用 2d 数组和字符串时似乎无法理解它们。
需要将数组输入方法和return排序的数组,格式相同。因此,对于数组中的每个条目,它将根据顺序数组的顺序对第 2 级 'file' 值进行排序。有什么建议可以实现吗?
//actual array to be sorted by 'file'
$Array (
[0] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant ));
//order array with desired sort order
$order = array("irritant","corrosive","environment" ,"health","toxic","oxidizing","flammable","explosive","gas");
//Desired Output Result
$Array (
[0] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic ) );
看到刺激性在前,毒性在后:)
基本方法是遍历数组和顺序数组并将键重新分配到新数组中...
<?php
$a[] = array('source' => 'img/table/icon/toxic.svg','file'=>'toxic');
$a[] = array('source' => 'img/table/icon/health.svg','file'=>'health');
$a[] = array('source' => 'img/table/icon/irritant.svg','file'=>'irritant');
$b = array();
$order = array("irritant","corrosive","environment","health","toxic","oxidizing","flammable","explosive","gas");
foreach ($a as $arr) {
foreach ($order as $key => $o) {
if ($o == $arr['file']) {
$b[$key] = $arr;
break;
}
}
}
ksort($b);
print_r(array_values($b)); // or print_r($b); if you dont want sequential keys
?>
输出:
Array (
[0] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic )
)
这可以通过巧妙地使用 PHP 的 usort 功能来实现。您的电话可能看起来像这样:
$order = array("irritant","corrosive","environment" ,"health","toxic","oxidizing","flammable","explosive","gas");
usort($array, function ($a, $b) use ($order) {
$aOrder = array_search($a['file'], $order);
$bOrder = array_search($b['file'], $order);
if ($aOrder == $bOrder) return 0;
return ($aOrder > $bOrder) ? 1 : -1;
});
我是 php 的新手,一直在尝试使用特定顺序数组对二维字符串数组进行排序。一直在考虑使用 'usort' 和 'comparison',但在使用 2d 数组和字符串时似乎无法理解它们。
需要将数组输入方法和return排序的数组,格式相同。因此,对于数组中的每个条目,它将根据顺序数组的顺序对第 2 级 'file' 值进行排序。有什么建议可以实现吗?
//actual array to be sorted by 'file'
$Array (
[0] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant ));
//order array with desired sort order
$order = array("irritant","corrosive","environment" ,"health","toxic","oxidizing","flammable","explosive","gas");
//Desired Output Result
$Array (
[0] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic ) );
看到刺激性在前,毒性在后:)
基本方法是遍历数组和顺序数组并将键重新分配到新数组中...
<?php
$a[] = array('source' => 'img/table/icon/toxic.svg','file'=>'toxic');
$a[] = array('source' => 'img/table/icon/health.svg','file'=>'health');
$a[] = array('source' => 'img/table/icon/irritant.svg','file'=>'irritant');
$b = array();
$order = array("irritant","corrosive","environment","health","toxic","oxidizing","flammable","explosive","gas");
foreach ($a as $arr) {
foreach ($order as $key => $o) {
if ($o == $arr['file']) {
$b[$key] = $arr;
break;
}
}
}
ksort($b);
print_r(array_values($b)); // or print_r($b); if you dont want sequential keys
?>
输出:
Array (
[0] => Array ( [source] => img/table/icon/irritant.svg [file] => irritant )
[1] => Array ( [source] => img/table/icon/health.svg [file] => health )
[2] => Array ( [source] => img/table/icon/toxic.svg [file] => toxic )
)
这可以通过巧妙地使用 PHP 的 usort 功能来实现。您的电话可能看起来像这样:
$order = array("irritant","corrosive","environment" ,"health","toxic","oxidizing","flammable","explosive","gas");
usort($array, function ($a, $b) use ($order) {
$aOrder = array_search($a['file'], $order);
$bOrder = array_search($b['file'], $order);
if ($aOrder == $bOrder) return 0;
return ($aOrder > $bOrder) ? 1 : -1;
});