[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Sorting by Alpha J SON array from API

Posted on 2011-09-15
4
Medium Priority
?
223 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 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