[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sort Json array in php

Posted on 2010-11-08
3
Medium Priority
?
1,600 Views
Last Modified: 2012-06-27
So say we have a json object

$json = '[{"date": "2011-08-07", "name": "bob"},{"date": "2011-07-07", "name": "jones"},{"date": "2011-09-03", "name": "blair"}]';

I can convert it over to an array

$arr = json_decode($json, true);

But i want to sort this by the date element so hypothetically if i did

foreach($arr as $thing)
{
echo ($thing['date']);
}

it would print by nearest event to furthest away
2011-07-07
2011-08-07
2011-09-03

whereas right now it would echo out like this:

2011-08-07
2011-07-07
2011-09-03
0
Comment
Question by:Brant Snow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Expert Comment

by:stilliard
ID: 34089285
0
 
LVL 8

Accepted Solution

by:
shanikawm earned 2000 total points
ID: 34089692

<?php
$json = '[{"date": "2011-08-07", "name": "bob"},{"date": "2011-07-07", "name": "jones"},{"date": "2011-09-03", "name": "blair"},{"date": "2009-01-01", "name": "test"}]';
$arr = json_decode($json, true);
print_r($arr);
function cmp($a, $b)
{
    return strcmp($a['date'], $b['date']);
}
usort($arr, "cmp");
print_r($arr);
?>

Open in new window

0
 
LVL 4

Author Closing Comment

by:Brant Snow
ID: 34094896
great
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question