Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
Good Afternoon all,

I am trying to work with arrays to display a list of school campuses within a given area after passing lat and lon in the url (?lat=43.19191&long=-72.2511).

Once I pull in their lat and lon I compare it against some database's and some algoritms to output the nearest campuses, but i'm having some trouble figuring out how to output it the way I want. This is what I WANT it to look like..

Code:
[Schools]
   [School]
   [name = schoolNameHere]
   [id = 125125121]
   [/School]
   
   [School]
   [name = schoolNameHere]
   [id = 215235]
   [/School]

   [School]
   [name = schoolNameHere]
   [id = 5322]
   [/School]

[/Schools]

To accomplish something like this should I use associative arrays in my php code? perhaps something like this in my for loop:
Code:
$data['name'][$start] = $row['Name'];

Or am i better off using array objects of some sort?

Any advice is greatly appreciated! Thanks
 
Its kind of hard to tell from your post exactly what you are trying to do without more snippets of your code, but your output has little to do with how you store your data in the array.

Your output example looks like some psudo-XML with the closing tags?

If the school IDs are unique I would use that as the index of your array, so you would do something like below (assuming I am not misinterpreting your question):

Code:
$data[$school_id]['id'] = $school_id;
$data[$school_id]['name'] = $school_name;
 
PHP:
<?
$data['125125121']['name'] = "School 1";
$data['215235']['name'] = "School 2";
$data['5322']['name'] = "School 3";

$string = "[Schools] \n<br />";

foreach ($data as $schoolID => $schoolData) {
	$string .= "[School] \n<br />";
	$string .= "[name = ".$schoolData['name']."] \n<br />";
	$string .= "[id = ".$schoolID."] \n<br />";
	$string .= "[/School] \n<br />";
}

$string .= "[Schools]";

echo $string;
?>

This will work for you. phantax is right with the id being the index value but you don't need to have ['id']. It's redundant.
In this scenario, you're using a multi-dimensional array. This allows you to add other values to each school. Like city, phone, state, or anything related to the school.

ex.
PHP:
$data['125125121']['name'] = "School 1";
$data['125125121']['phone'] = "800-555-1234";
$data['125125121']['city'] = "The City";
 
Last edited:
You are right, I was just showing he can have multiple values stored under the index. I was assuming he would put his lat and long values there or any future data required.

Also, it could simplify his output code by echoing out the key name with the value, rather than explicitly adding a line echoing each new value in the output.

Code:
foreach($data as $data_key => $data_val){
   echo($data_key . " = " . $data_val);
}
 
Thanks both of you for your responses, however, i'm still not able to accomplish what I'm trying to.

My ultimate goal is to return a JSON string with this sort of structure:
This may not be the right JSON syntax but thats the idea..

Code:
{"Name":"Fairfield University","Name":"Sacred Heart University","Name":"University of Bridgeport"}

The way this is being used, is the website is being called by an iphone application, that sends lat and lon as a paramater, and the website returns a json string of the nearest schools, here is my code:
Code:
<?
include("config.php");
require_once('zipcode.class.php');      // zip code class

//get values from iphone
$lat = $_GET['lat'];
$lon = $_GET['lng'];

$start = 0;
$data = array();

$page = file_get_contents("http://api.geonames.org/findNearbyPostalCodesJSON?lat=" . $lat . "&lng=" . $lon . "&username=username");
$results = json_decode($page);
$z = new zipcode_class;

foreach ($results->postalCodes as $nearby) {
    $zipcode = $nearby->postalCode;
    $zips = $z->get_zips_in_range($zipcode, '5', _ZIPS_SORT_BY_DISTANCE_ASC, true); 
	 foreach ($zips as $zipcode => $value) {
	
		$query1  = "SELECT * FROM colleges WHERE uniZipcode = '$zipcode'";
		$result1 = mysql_query($query1);
		while($row = mysql_fetch_array($result1))
			{
			$data[$start] = $row['Name'];
			$start++;
			
			}
	
	 }

    
}

//remove dups
$final = array_unique($data);
echo json_encode($final);
?>
[/CODE]

The above code returns this,
{"0":"Fairfield University","1":"Sacred Heart University","6":"University of Bridgeport"}

I tried to implement the previous suggestions but was unsuccessful, can anyone shed any light on this?

Thank you very much!
 
From what I can tell you just want the ID of each school to be replaced by "Name"? You can't do that in an array. You're wanting to replace the index with a common string value. At least that's what I'm understanding. Why are you not able to have the index value?
 
From what I can tell you just want the ID of each school to be replaced by "Name"? You can't do that in an array. You're wanting to replace the index with a common string value. At least that's what I'm understanding. Why are you not able to have the index value?


The script will be called in an iPhone application i'm trying to develop, I need to use a JSON parser in ObjectiveC and it uses the json call,
Code:
dictionary objectForKey:@:"Name".

Maybe i'm approaching this in the wrong way..?
 
Can you provide an example of the complete JSON string / format you are expecting to generate and send?
 
If you don't need multiple values for each index, just use the name of the school as the index of the array. This way you won't need the array unique function.

You cannot have the index be identical for each element, so what you are trying to do is not really going to work, just prepare the JSON string yourself by looping the array, appending the text as necessary to fit your required format and return it.

Something like:

PHP:
$data[trim($row['Name'])] = trim($row['Name']);

...

$json_string = '{';
foreach($data as $data_key => $data_val){

   $json_string .= '"Name":"' . $data_val . '",';

}
$json_string = substr($json_string, 0, -1);
$json_string .= '}';

echo $json_string;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.