我有一个数组如下:
$people = array(
200 => array(
'id' => 12345,
'first_name' => 'Joe',
'surname' => 'Bloggs',
'age' => 23,
'sex' => 'm'
),
100 => array(
'id' => 12346,
'first_name' => 'Adam',
'surname' => 'Smith',
'age' => 18,
'sex' => 'm'
),
500 => array(
'id' => 12347,
'first_name' => 'Amy',
'surname' => 'Jones',
'age' => 21,
'sex' => 'f'
)
我想在第一个值上对此数组进行排序,您可以看到它是200,100和500.
答案 0 :(得分:2)
这应该简单如下:
GameScene
要按实际数值排序(要使ksort($people);
在1000
之后而不是500
之后),请使用:
100
ksort()
按键对数组进行排序。
例如:
ksort($people, SORT_NUMERIC);
答案 1 :(得分:2)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
</LinearLayout>
ksort($people);
答案 2 :(得分:2)
$result = array(
array('name' => 'Tahir', 'age' => '40'),
array('name' => 'Usman', 'age' => '30'),
array('name' => 'Danish', 'age' => '20'),
array('name' => 'Aneeq', 'age' => '10')
);
echo '<pre>';
print_r($result);
echo '</pre>';
foreach ($result as $key => $row) {
$name[$key] = $row['name'];
$age[$key] = $row['age'];
}
array_multisort($age, SORT_ASC, $result);
echo '<pre>';
print_r($result);
echo '</pre>';
echo "<pre>";
echo "Name\t\tAge";
foreach ( $result as $var ) {
echo "\n", $var['name'], "\t\t", $var['age'];
}
答案 3 :(得分:0)
您可以尝试此解决方案。
<?php
$people = array(
200 => array(
'id' => 12345,
'first_name' => 'Joe',
'surname' => 'Bloggs',
'age' => 23,
'sex' => 'm'
),
100 => array(
'id' => 12346,
'first_name' => 'Adam',
'surname' => 'Smith',
'age' => 18,
'sex' => 'm'
),
500 => array(
'id' => 12347,
'first_name' => 'Amy',
'surname' => 'Jones',
'age' => 21,
'sex' => 'f'
));
ksort($people, SORT_NUMERIC);
print_r($people);
?>
答案 4 :(得分:0)
您可以根据您的要求使用以下两个功能: -
ksort() - 根据键
按升序对关联数组进行排序krsort() - 根据键
按降序对关联数组进行排序如何使用它: -
ksort($人);
krsort($人);