我有这个数组:
[0] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #20
)
)
[1] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #5
)
)
[2] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => #9, MusicVideos
)
)
[3] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => #12, MusicVideos
)
)
现在我想通过 [tags] 对这个数组进行排序,我该如何做这个 PHP?使用在线工具 [标签] 在我的网站上按我想要的“#1”、“#2”订购
但我比 [Tags] 多一个,例如“#1”和“Category”
[Tags] => #1
[Tags] => MusicVideos, #2
[Tags] => #3
可以这样订购吗?
我尝试使用此功能:
function msort($array, $key, $sort_flags = SORT_NUMERIC) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// @TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
但是这个 SORT by ALL 在 NAME 和下一个 NUMBER 之后引入了 EMPTY
[0] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => #5
)
)
[1] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => Commercials, #3
)
)
[2] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => Commercials, #9
)
)
[3] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #1
)
)
[4] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #10
)
)
我想要这样的东西:
[0] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #1
)
)
[1] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => Commercials, #3
)
)
[2] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => #5
)
)
[3] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => Commercials, #9
)
)
[4] => Array
(
[tags] => SimpleXMLElement Object
(
[0] => MusicVideos, #10
)
)
谢谢