我有一个类似这样的数组:
$array[] = array(
"name" => "sample",
"image" => "sample.jpg",
"header" => "sampleDelights",
"intro_copy" => ""
);
$array[] = array(
"name" => "lwmag",
"image" => "lwmag.jpg",
"header" => "LW Mag",
"intro_copy" => ""
);
我想根据PHP中关键字“header”的字母顺序对此数组进行排序。我试过usort并搜索内置函数但找不到(或者看过去)。这可能只用一个php函数吗?
答案 0 :(得分:0)
看看" ksort"
对于按键排列数组非常有用。
<?php
$sample = array("d" => "maximum", "a" => "minimum", "c" => "mid")
ksort($sample);
foreach ($sample as $key => $val) {
echo "$key = $val \n";
}
?>
将显示
a = minimum
c = mid
d = maximum
希望这是你追求的目标。
来源:PHP Manual
答案 1 :(得分:0)
function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}