2013-04-03 59 views
0
$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" 
} 
+3

'$ value'是一個數組。它需要是一個標量變量。 –

+2

您不能將數組用作數組鍵。你的意思是使用'id'鍵嗎? (如'$ sorted_data [$ value ['id']] [] = $ images' –

+0

謝謝! – Tuccinator

回答

1

一個很好的辦法做到在一個關鍵的排序多維數組無需知道您在陣列中首先有哪些鍵:

<?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); 
+0

哇,謝謝!我最近剛剛問過這個問題! – Tuccinator