This is a collection of questions that come up every now and then about syntax in PHP. This is also a Community Wiki, so everyone is invited to participate in maintaining this list.
It used to be hard to find questions about operators and other syntax tokens.¹
The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from …
可能重复:
如何在PHP中对数组和数据进行排序?
如何在
PHP中排序多维数组PHP排序数组按SubArray值PHP 按值
排序多维数组
我的数组看起来像:
Array(
[0] => Array(
[name] => Bill
[age] => 15
),
[1] => Array(
[name] => Nina
[age] => 21
),
[2] => Array(
[name] => Peter
[age] => 17
)
);
Run Code Online (Sandbox Code Playgroud)
我想根据他们的名字按字母顺序对它们进行排序.我看到PHP排序数组按SubArray值,但它没有多大帮助.任何想法如何做到这一点?
我需要按照getNotifications函数中值'start_date'的DESC顺序对通知数组进行排序:
$posts_id_arr = getPoststIds($conn);
foreach ($posts_id_arr as $key => $val) {
$total_arr[$key] = [
'notification' => getNotifications($val['post_id'], $user_id, $conn)
];
}
$response_array = array('data' => $total_arr, 'more things' => $more_array);
echo json_encode($response_array);
Run Code Online (Sandbox Code Playgroud)
现在,由于foreach循环,订单是通过邮递ID进行的。
data {
notification:
[
{
post_id: “1",
start_date: "2016-10-10 08:00:00",
},
{
post_id: “1",
start_date: "2016-10-10 12:00:00",
}
],
notification:
[
post_id: “2",
start_date: "2016-10-10 09:00:00",
},
{
post_id: “2",
start_date: "2016-10-10 13:00:00",
}
]
}
Run Code Online (Sandbox Code Playgroud)
我需要是1:08:00,2:09:00,1:12:00,2:13:00