Solved

Sorting by Alpha J SON array from API

Posted on 2011-09-15
4
216 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
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 109

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

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

808 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