Php 按时间戳的数组过滤器
我正在尝试按时间戳过滤数组,但没有成功。这是我的密码:Php 按时间戳的数组过滤器,php,arrays,sorting,Php,Arrays,Sorting,我正在尝试按时间戳过滤数组,但没有成功。这是我的密码: $get = array{ "5656778": { "date": 1541304426, "text": "some text", }, "567845": { "date": 1541304416, "text": "some other text", }, } function cmp($b, $a){ $ad = str
$get = array{
"5656778": {
"date": 1541304426,
"text": "some text",
},
"567845": {
"date": 1541304416,
"text": "some other text",
},
}
function cmp($b, $a){
$ad = strnatcmp($a['date']);
$bd = strnatcmp($b['date']);
return ($bd-$ad);
}
usort($get, 'cmp');
foreach ($get as $key => $value) {
// displaying results
}
我试图修正这个方法,但结果都是一样的
我错过了什么
非常感谢您的帮助我想您提出的问题实际上是您正在转换为数组的完整
JSON
数据的一部分。在这种情况下,此代码将为您提供所需的结果:
$json = '{
"5656778": {
"date": 1541304426,
"text": "some text"
},
"567845": {
"date": 1541304416,
"text": "some other text"
}
}';
$get = json_decode($json, true);
usort($get, function ($a, $b) { return $a['date'] - $b['date']; });
print_r($get);
输出:
Array
(
[0] => Array
(
[date] => 1541304416
[text] => some other text
)
[1] => Array
(
[date] => 1541304426
[text] => some text
)
)
我认为您在问题中所提出的实际上是您正在转换为数组的完整
JSON
数据的一部分。在这种情况下,此代码将为您提供所需的结果:
$json = '{
"5656778": {
"date": 1541304426,
"text": "some text"
},
"567845": {
"date": 1541304416,
"text": "some other text"
}
}';
$get = json_decode($json, true);
usort($get, function ($a, $b) { return $a['date'] - $b['date']; });
print_r($get);
输出:
Array
(
[0] => Array
(
[date] => 1541304416
[text] => some other text
)
[1] => Array
(
[date] => 1541304426
[text] => some text
)
)
这不是一个数组。这是json…@Andreas实际上在php中什么都不是。是的,我刚刚注意到了。乍一看,它看起来像json。但后来我注意到你能帮我修一下吗?再说,“它坏了”并不是什么问题。您应该指定得到的错误类型。这不是数组。这是json…@Andreas实际上在php中什么都不是。是的,我刚刚注意到了。乍一看,它看起来像json。但后来我注意到你能帮我修一下吗?再说,“它坏了”并不是什么问题。您应该指定得到的错误类型。