?
Solved

Sorting by Alpha J SON array from API

Posted on 2011-09-15
4
Medium Priority
?
219 Views
Last Modified: 2012-08-13
I am trying to sort a JSON array by "resortName". I have the JSON output here: http://pastie.org/2539795

I am parsing my JSON with PHP using:

<?php 
$type = $_GET["type"];
$jsonurl = "http://feeds.snocountry.com/conditions.php?apiKey=skimaine$54.maineski118&states=ME&resortType=".$type."";
$json_string = file_get_contents($jsonurl,0,null,null);
echo sort(json_string);
?>

Open in new window


I am trying to sort by this item in the array : "resortName": "Black Mountain" by alpha.

Any ideas?

Thanks.
0
Comment
[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
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
R-Byter earned 2000 total points
ID: 36548384
Here you go:

<?php
	error_reporting(E_ALL);
	
	function cmp($a, $b)
	{
		if (strtolower($a->resortName) == strtolower($b->resortName)) {
			return 0;
		}
		return (strtolower($a->resortName) < strtolower($b->resortName)) ? -1 : 1;
	}

	$json_string = file_get_contents("json.txt");
	$json_string = json_decode($json_string);
	usort($json_string->items, 'cmp');	
	$json_string = json_encode($json_string);
	
	echo $json_string;
?>

Open in new window


You should use PHP usort and your own defined comparison function.
When you get the string with file_get_contents, decode it (convert to object). Use usort to sort it by defined item and re-encode it to json.

Regards
0
 
LVL 14

Expert Comment

by:R-Byter
ID: 36548391
For this example I took JSON from your post and made json.txt of it.

Regards
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36548529
Usort() is the right answer.  It sorts arrays, not strings, and that is why json_decode() is needed to recreate the array.
http://php.net/manual/en/function.usort.php

But please do us a favor going forward.  Please use the EE functionality for file uploads and code snippets.  It's much easier to get access to the tools we need to answer your questions if we don't have to deal with external sites like "pastie."

This page might also be useful...
http://php.net/manual/en/array.sorting.php
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
ID: 36718114
Worked perfect, thank you! Sorry for the delay on answering.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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