16
Tutorials, References, and Examples / [PHP] Organizing a JSON Array
« on: April 12, 2010, 02:46:52 am »
This is a neat little script that'll take a series of array objects, such as coordinate pairs, [123,123] and take all the nonsense out, and format them pretty, 4 to a row, lined up with tabs. I'm using it to get all the coordinates of mining nodes off of WoW head into an array for.. purposes. This could pretty easily be changed to sort any kind of array, though.
Code: [Select]
<?php
if(!isset($_POST['array'])) {
printf('<form act=' . $_SERVER['PHP_SELF'] . ' method="POST">');
printf('<textarea name="array" cols="100" rows="25"></textarea><br />');
printf('<input type="submit" name="submit" />');
printf('</form>');
} else {
$data = str_replace(" ", "", $_POST['array']);
preg_match_all("(\[.*\],)", $data, $data);
$data = explode("],[", implode("", $data[0]));
printf("<pre>");
for ($i = 0; $i < count($data); $i += 4) {
printf("[%s],\t", $data[$i + 0]);
printf("[%s],\t", $data[$i + 1]);
printf("[%s],\t", $data[$i + 2]);
printf("[%s]\n", $data[$i + 3]);
}
printf("</pre>");
}
?>