• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error sorting array

#1
[eluser]karloff[/eluser]
hi,

I'm trying to sort an array bu the distance but having a few problems, the following error keep happening

Quote:Cannot use object of type stdClass as array....

here is my code

Code:
function sorter($a, $b)
{
    if ($a['distance'] == $b['distance'])
    {
        return 0;
    }
    return ($a['distance'] < $b['distance']) ? -1 : 1;
}
    usort($array,  'sorter');
    print_r($array);

and this is the structure of the array

Code:
Array ( [0] => stdClass Object ( [id] => 16 [name] => name [address] => some address here [postcode] => BD3 9NB [telephone] => 9893213 [body] =>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. In rutrum tincidunt massa viverra aliquam. Aenean ut sodales massa. Ut ac neque sit amet nulla eleifend condimentum. Ut cursus aliquet est, sit amet laoreet sem condimentum et. Fusce blandit ultrices lorem id rhoncus. Vivamus cursus egestas augue at blandit. Donec accumsan pulvinar mi, malesuada sodales turpis malesuada at. Mauris hendrerit fermentum nisl, eu ullamcorper neque mollis eget. Suspendisse imperdiet fringilla ultrices. Donec massa augue, consequat sed mollis sit amet, vestibulum consectetur lacus. Quisque a nisi non nisi faucibus dictum. Aenean imperdiet, nulla ac pulvinar cursus, nibh est suscipit dui, ac vulputate turpis dolor ullamcorper odio. Pellentesque rhoncus erat suscipit felis faucibus volutpat. Praesent risus libero, convallis vitae ultricies in, hendrerit ut velit. Sed velit metus, mattis vitae gravida et, cursus at elit. Maecenas sapien nisl, sollicitudin semper tempor at, pretium eget metus.
[Lat] => 9.9999 [Long] => -1.7270 [image] => DSCN1854.JPG [distance] => 2 )


.....


etc, etc

how can i access the data to sort, anyone got any pointers?

#2
[eluser]karloff[/eluser]
sorted

Code:
function sorter($a, $b)
{
    if ($a->distance == $b->distance)
    {
        return 0;
    }
    return ($a->distance < $b->distance) ? -1 : 1;
}

i'm sure i tried that, totally sure, anyway it works perfect... bah, must be tired Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.