php - 偏移类型非法?

标签 php mysql arrays timestamp

$images = valley_images();
var_dump($images);
$sorted_data = array();

foreach($images as $key => $value) {
    if ($key == 'timestamp') {
        $sorted_data[$value][] = $images;
    }
}

ksort($sorted_data);

错误出现在这一行:

$sorted_data[$value][] = $images;

当我对图像进行 var 转储时,我收到以下消息:

array(2) { 
[0]=> array(2) {
 ["id"]=> string(2) "17" ["timestamp"]=> string(10) "1359797773" 
} 
[1]=> array(2) {
 ["id"]=> string(2) "20" ["timestamp"]=> string(10) "1359934365"
} 

最佳答案

一种在多维数组上对键进行排序的好方法,而无需先知道数组中有哪些键:

<?php 
$people = array( 
array("name"=>"Bob","age"=>8,"colour"=>"red"), 
array("name"=>"Greg","age"=>12,"colour"=>"blue"), 
array("name"=>"Andy","age"=>5,"colour"=>"purple")); 

var_dump($people); 

$sortArray = array(); 

foreach($people as $person){ 
    foreach($person as $key=>$value){ 
        if(!isset($sortArray[$key])){ 
            $sortArray[$key] = array(); 
        } 
        $sortArray[$key][] = $value; 
    } 
} 

$orderby = "name"; //change this to whatever key you want from the array 

array_multisort($sortArray[$orderby],SORT_DESC,$people); 

var_dump($people); 

关于php - 偏移类型非法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776864/

相关文章:

php - 我的密码接受大小写字母

php - 网页不打印winsock信息

mysqld.exe - Laragon 启动时出现应用程序错误

java - 如何在java中自动填充数组?

android - 如何解析四方类别json

javascript - 查找数组中的数字是否介于两个数字之间并输出介于两者之间的值的数量

php - 如何在 php 脚本中回复 POST?

php - 如何从 MySQL 数据库获取时间戳?

php - 使用包含的 div 高度缩放容器 div

php - 使用 CodeIgniter 从 mysql 数据库中随机记录