Solved

Sorting by Alpha J SON array from API

Posted on 2011-09-15
4
213 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
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
R-Byter earned 500 total points
Comment Utility
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
Comment Utility
For this example I took JSON from your post and made json.txt of it.

Regards
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Worked perfect, thank you! Sorry for the delay on answering.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now