如果沒有一個參考點來確定哪一個是最老的用戶,您可能會進行多循環比較或排序然後插入。
的多環comparasin看起來像
<?php
$array = [
["name" => "Jon", "age" => 44],
["name" => "Bob", "age" => 32],
["name" => "Jim", "age" => 103],
["name" => "Ted", "age" => 19]
];
$count = count($array);
for($index = 0; $index < $count; ++$index) {
$order = 1;
$age = $array[$index]["age"];
for($index2 = 0; $index2 < $count; ++$index2) {
if($array[$index2]["age"] > $age) {
++$order;
}
}
$array[$index]["order"] = $order;
}
echo "<pre>";
var_dump($array);
echo "</pre>";
排序,然後插入將涉及array_walk
和uasort
從docs
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
$item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
echo "$key. $item2<br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
和docs
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
所以你的例子會是這樣的:
<?php
function walkFunc(&$item, $key) {
$item["order"] = $key + 1;
}
// as @waterloomatt mentioned the Spaceship operator is meant for this
function sortComp($a, $b) {
return ($a["age"] <=> $b["age"]) * -1;
}
$array = [
["name" => "Jon", "age" => 44],
["name" => "Bob", "age" => 32],
["name" => "Jim", "age" => 103],
["name" => "Ted", "age" => 19]
];
uasort($array, 'sortComp');
array_walk($array, 'walkFunc');
echo "<pre>";
var_dump($array);
echo "</pre>";