Tuccinator
Tuccinator

Reputation: 67

Illegal Offset Type?

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

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

ksort($sorted_data);

The error is appearing on this line:

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

When I do the var dump of images I receive this:

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

Upvotes: 0

Views: 873

Answers (1)

Jhonathan H.
Jhonathan H.

Reputation: 2713

A nice way to do sorting of a key on a multi-dimensional array without having to know what keys you have in the array first:

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

Upvotes: 1

Related Questions