Php 非法的偏移类型?
错误出现在这一行:Php 非法的偏移类型?,php,mysql,arrays,timestamp,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); 当我对图像进行var转储时,我收到以下信息: $s
$images = valley_images();
var_dump($images);
$sorted_data = array();
foreach($images as $key => $value) {
if ($key == 'timestamp') {
$sorted_data[$value][] = $images;
}
}
ksort($sorted_data);
当我对图像进行var转储时,我收到以下信息:
$sorted_data[$value][] = $images;
一种在多维数组中对键进行排序的好方法,无需首先知道数组中有哪些键:
array(2) {
[0]=> array(2) {
["id"]=> string(2) "17" ["timestamp"]=> string(10) "1359797773"
}
[1]=> array(2) {
["id"]=> string(2) "20" ["timestamp"]=> string(10) "1359934365"
}
$value
是一个数组。它必须是标量变量。不能将数组用作数组键。您的意思是使用它的id
键吗?(就像在$sorted_data[$value['id']][]=$images
中一样,你们俩都帮了大忙,谢谢!
<?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);