Author Topic: [PHP] Organizing a JSON Array  (Read 20205 times)

0 Members and 1 Guest are viewing this topic.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
[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>");
        
    }
    
?>
« Last Edit: April 12, 2010, 02:06:06 pm by Joe »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing an Array
« Reply #1 on: April 12, 2010, 07:17:32 am »
asort/ksort + print_r

Stuff is built in for a reason :d

[edit]
1. Stop using printf, it's in PHP so you can auto typecast.
2. Stop using double quote strings for constant strings.
3. Stop using printf when you don't give it any arguments!
4. If you don't check preg_match_all result you might end up with null.
5. I just tested your code, and all it prints out is:
Code: [Select]
[], [], [], []
This will work much better, and it even does multiple levels of arrayness (though without tabs), and it just prints 1 item per line:
Code: [Select]
<?php

if(!isset(
$_POST['array']))
{
print '<form act=' $_SERVER['PHP_SELF'] . ' method="POST">';
print '<textarea name="array" cols="100" rows="25"></textarea><br />';
print '<input type="submit" name="submit" />';
print '</form>';
} else {
print '<pre>';
foreach(preg_split('/\][, \t]*\[/'$_POST['array']) as $item)
print (substr($item01) != '[' '[' '') . $item . (substr($item, -1) != ']' ']' '') . "\n";
print '</pre>';
}

?>
« Last Edit: April 12, 2010, 07:48:55 am by rabbit »

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Organizing an Array
« Reply #2 on: April 12, 2010, 12:06:19 pm »
Put in something like:

Code: [Select]
65: {
0: { count: 11, coords: [[8,42.5],[8,43.6],[8.1,42.4],[8.8,41.3],[9.8,41.1],[28.4,42.8],[29,42],[32.1,20.2],[32.4,21.5],[32.5,21.5],[34.6,33.8]] }
}
,67: {
0: { count: 253, coords: [[22.8,63.5],[23.4,56.2],[23.5,56.2],[24.1,48.2],[24.8,62],[24.8,69.3],[25,46.2],[25,57.5],[25.1,67],[25.2,44.5],[25.2,47.1],[25.2,49.4],[25.2,49.5],[25.3,68.3],[25.4,42.8],[25.8,58.2],[26,68.3],[26.1,67.4],[26.3,68.9],[26.6,67.3],[27,47.7],[27.1,50.4],[27.5,50],[27.6,58.4],[27.6,58.5],[28.1,61.3],[28.9,40.3],[29,43.7],[29,46.2],[29.2,63.9],[29.7,51.6],[29.8,46.8],[29.8,67.3],[29.9,42.9],[29.9,43.5],[29.9,45.3],[30,41.5],[30,64.1],[30.2,94.7],[30.4,45.7],[30.7,84.1],[30.7,86.3],[30.8,67.3],[30.9,42.7],[31,46.7],[31.4,65.2],[31.5,43.9],[31.8,81.8],[33,50.3],[33,67.3],[33.5,65.1],[33.8,39],[34.4,66.4],[34.4,71.2],[34.5,71.2],[34.6,47.8],[34.6,82.9],[34.6,88.7],[35.1,81.8],[35.2,67.9],[35.4,41.7],[35.4,66.8],[35.7,90],[35.9,63.8],[35.9,80.9],[36,68.5],[36,70.4],[36.1,68.4],[36.2,42.4],[36.2,42.5],[36.8,67.1],[36.9,44],[36.9,81.3],[37.2,67.6],[37.4,54.6],[37.5,69.8],[38.2,74.9],[38.2,80.2],[38.8,90],[39,88.3],[39.6,88.4],[39.7,75.9],[39.9,62.7],[39.9,74],[40,77.6],[40.1,50.1],[40.1,88.7],[40.4,74.5],[40.6,76.5],[40.9,73.6],[41,61],[41.2,51.2],[41.4,64.9],[41.5,74.1],[41.7,44],[41.9,66.2],[42.1,62],[42.1,64.4],[42.2,62.8],[42.5,32.9],[42.5,74.1],[42.6,63.8],[43,67.9],[43,74.7],[43.1,75.9],[43.2,76.5],[43.3,65.4],[43.3,69.9],[43.4,70.5],[43.5,74.7],[43.6,62.2],[43.7,43.6],[43.7,77.4],[43.8,68.3],[43.8,69.9],[43.9,60],[44,42.8],[44.1,34.3],[44.2,63.4],[44.2,63.5],[44.3,70.6],[44.3,77.5],[44.4,66.8],[44.5,40.1],[44.5,73.1],[44.7,67],[44.8,67.6],[44.9,76],[45.1,54.6],[45.1,74.1],[45.2,70.9],[45.7,64.9],[46,77.3],[46.1,73.6],[46.3,59.6],[47,77],[47.3,69.6],[47.4,72.6],[47.7,73],[48.1,74],[48.3,71],[48.4,71.7],[48.6,78.9],[48.8,73],[48.8,74.4],[49.6,33.8],[50.1,70.2],[50.2,71.1],[52.6,69.5],[52.9,65.8],[53.7,62],[53.7,63.2],[53.8,65],[54.1,61.2],[54.3,66.3],[55,71.2],[55,71.8],[55.1,62.3],[55.3,60.2],[55.3,63.9],[55.4,62.5],[55.8,64.1],[56,62.2],[56.7,63.2],[57,46.2],[57,62.1],[57.3,41.5],[57.4,44.1],[57.4,60.9],[57.5,60.9],[57.5,65.2],[57.7,42.6],[58,59],[58.1,70.2],[58.4,67.3],[58.7,47],[59,58.2],[59.1,65.3],[59.6,46.1],[59.7,42.4],[59.7,42.5],[60.2,57.5],[60.5,67.5],[60.8,66.8],[61,40.8],[61.8,46.6],[62.9,62.7],[63,59.5],[63.1,48],[63.3,64.2],[63.7,40.2],[63.8,58.9],[64.1,39.2],[64.2,65.8],[64.4,59.5],[64.5,49.4],[64.6,64.6],[64.8,41.5],[64.8,63.6],[64.9,39],[65,44.9],[65.2,59],[65.5,70.5],[65.7,63.6],[65.8,44.2],[66,42.8],[66,49.4],[66,61.9],[66.1,42],[66.4,40.1],[66.4,57.6],[66.4,58.9],[66.7,42.7],[66.9,60.9],[67,58.2],[67.1,43.7],[67.5,43.4],[67.6,63.4],[67.6,63.5],[67.7,48.2],[68,42.2],[68,58.3],[68.1,44.4],[68.1,44.5],[68.6,43.9],[68.7,40.7],[68.7,53.3],[68.8,41.9],[68.8,63.7],[69.4,51.1],[69.6,63.5],[69.7,50.9],[69.8,51.5],[71.2,63.9],[71.9,46.4],[71.9,55.3],[72.1,58.9],[72.2,57.3],[72.6,61.5],[72.9,52.6],[73.4,49.6],[74.1,70.3],[75.4,64.3],[75.5,64.3],[75.7,62.5],[76.8,61.8],[76.9,60.8],[77,60.2],[77,64.4],[77,64.5],[77.8,62.8],[80.1,72.6],[82,70.6]] }
}
,210: {
0: { count: 469, coords: [[27.5,63],[28,65.7],[28.8,62.7],[28.9,59.4],[29,64.3],[30.1,41.6],[30.3,41.2],[30.3,59],[30.8,69.4],[30.8,70],[30.9,56.3],[31.2,71.4],[31.3,35.6],[31.4,33.5],[31.4,49.1],[31.4,98.2],[31.5,33.5],[31.8,45.8],[32.2,34.9],[32.2,59.8],[32.2,96.1],[32.3,66.5],[33,59.5],[33.3,37.8],[33.3,71.2],[33.4,47],[33.4,66.6],[33.5,66.6],[33.7,47.7],[33.8,47.2],[33.9,29.9],[34.2,29.3],[34.5,71.6],[34.8,26.3],[34.8,67.7],[35.1,67.4],[35.1,69.9],[35.4,89],[35.5,89.1],[35.7,67.1],[35.8,28.7],[36.1,42.2],[36.1,66.4],[36.2,55.4],[36.2,55.5],[36.2,65.3],[36.3,52],[36.4,91.1],[36.5,65.9],[36.6,43.5],[36.6,52.3],[36.6,55.5],[36.7,55.3],[36.7,65.4],[36.7,92.6],[37,62.5],[37.1,42.5],[37.2,62.3],[37.4,34.6],[37.4,41.3],[37.5,34.5],[37.5,66.4],[37.6,48],[37.6,66.9],[37.7,34.2],[37.8,40.1],[37.9,57.4],[38.2,27.1],[38.2,38.4],[38.2,38.5],[38.2,55.4],[38.2,55.5],[38.2,67.8],[38.4,37.3],[38.5,27.1],[38.5,55.3],[38.5,56],[38.6,50.1],[38.8,26.3],[38.9,35.2],[39,57.9],[39.1,79.7],[39.2,37.6],[39.2,40],[39.2,90.2],[39.6,34.9],[39.6,48.9],[39.6,70.3],[39.7,87.3],[39.9,34.4],[39.9,48.3],[39.9,71.4],[40,36.6],[40,43.3],[40,63.7],[40.3,62.7],[40.5,37.5],[40.9,71.8],[41,41.4],[41,41.5],[41,64.4],[41,64.5],[41.1,48.5],[41.2,32.9],[41.2,46.3],[41.2,46.6],[41.2,48.4],[41.2,60.2],[41.3,36.9],[41.4,35.9],[41.4,44.4],[41.4,44.5],[41.4,52.9],[41.5,72.3],[41.6,53.1],[41.8,47.1],[41.9,34.1],[41.9,85.8],[42.1,35.6],[42.2,59.8],[42.3,35],[42.3,42.5],[42.4,42],[42.6,63],[42.7,38.2],[42.9,61.4],[42.9,61.5],[43.2,67.4],[43.3,46.9],[43.3,63.9],[43.4,46.4],[43.5,39.7],[43.9,63.8],[44.1,46.3],[44.7,37.2],[44.8,38],[44.9,26.8],[44.9,27.9],[45,56.2],[46.1,46.4],[46.1,94.7],[46.2,54.1],[46.4,47.1],[46.5,36.3],[46.6,37.6],[46.6,54.2],[46.6,83],[46.7,46.3],[46.8,54.6],[46.8,57.3],[46.9,37.4],[46.9,46.8],[47,71.1],[47,71.5],[47,81.1],[47.2,33.6],[47.2,50.2],[47.3,62.1],[47.5,52.2],[47.5,57.7],[47.5,70.4],[47.6,70.5],[47.7,81.3],[47.9,35.9],[47.9,53.4],[48,49.2],[48.1,57.4],[48.2,58.5],[48.4,36.6],[48.4,82.9],[48.5,31.9],[48.5,52.3],[48.5,58.8],[48.5,84.1],[48.6,36.4],[48.6,61.6],[48.7,56.4],[48.9,60.8],[49,56.9],[49,63.5],[49.1,30.6],[49.3,60.4],[49.4,63],[49.5,63.9],[49.5,82.1],[49.6,53.9],[49.7,81.1],[49.8,98.1],[49.9,46.4],[49.9,49.1],[50,47],[50,50.1],[50.4,53.1],[50.4,87.8],[50.6,35.6],[50.6,45.6],[50.6,53.5],[50.6,86.6],[50.8,85.7],[50.9,50.3],[51.3,42.4],[51.3,42.5],[51.5,87],[51.7,68.7],[51.9,62.8],[52,62.3],[52,90.2],[52,98.7],[52.1,69.5],[52.4,84.6],[52.5,45.1],[52.6,70.2],[53.5,27.1],[53.6,27.8],[53.6,87.4],[53.6,87.9],[53.6,91.5],[53.7,21.9],[54.1,59.9],[54.1,60.8],[54.2,23.9],[54.2,88.9],[54.5,25.1],[54.8,77.9],[54.8,79.5],[55,84],[55.4,23.4],[55.4,44.3],[55.5,23.4],[55.8,44],[55.8,84.9],[55.9,24.9],[55.9,85.5],[56,90.6],[56.1,35.1],[56.3,83.3],[56.3,84.4],[56.4,34],[56.4,44.5],[56.5,33.3],[56.5,90],[56.6,70.9],[56.7,43.9],[57,23.6],[57,24.8],[57.1,44.8],[57.1,85.7],[57.3,82.7],[57.4,28],[57.4,45.7],[57.4,76.1],[57.5,28],[57.7,39.4],[57.8,87.5],[57.9,38.3],[57.9,40.5],[57.9,46.4],[58,33.1],[58,68.7],[58.1,33.9],[58.2,24.8],[58.3,35.7],[58.3,42.4],[58.3,82],[58.4,40.3],[58.4,85.7],[58.5,40.3],[58.8,27.7],[58.9,43.6],[58.9,73.6],[59,33.4],[59,33.5],[59.3,30.9],[59.5,42.2],[59.6,35.4],[59.6,35.5],[59.8,22.4],[60,73.1],[60,74.1],[60.1,23.4],[60.1,23.5],[60.6,32.3],[60.6,44.4],[60.6,44.5],[60.7,89.5],[61,23.6],[61.6,35.7],[62,75.8],[62.1,48.6],[62.3,30.3],[62.3,34.6],[62.3,36.7],[62.3,63],[62.4,39.1],[62.4,51.1],[62.5,39.1],[62.5,50.4],[62.5,51.2],[62.5,65.4],[62.5,65.5],[62.7,47.7],[62.7,51.7],[62.7,52.5],[62.9,39.5],[62.9,80.5],[63,37.9],[63.1,54.6],[63.4,59.1],[63.4,60.1],[63.5,31],[63.5,47.9],[63.5,51],[63.6,64.4],[63.7,55.2],[63.7,81.2],[63.8,22.7],[63.8,35.6],[63.8,55.5],[63.8,85.1],[63.9,34.2],[64,20.3],[64,33.1],[64,39.9],[64.1,40.8],[64.3,37.5],[64.4,57.1],[64.7,41.6],[64.8,32.4],[64.8,32.5],[64.8,56.6],[65,56.4],[65.3,64.9],[65.5,57.1],[65.7,64],[65.7,64.8],[65.9,61.4],[65.9,61.5],[65.9,66.1],[66,30.1],[66,40.7],[66,46.8],[66,90.3],[66.1,62.7],[66.2,21.7],[66.2,40.2],[66.2,67.2],[66.3,74.5],[66.4,29.2],[66.4,47.7],[66.4,69.5],[66.5,47.7],[66.7,28.3],[66.7,35.4],[66.7,41.6],[66.8,35.5],[66.8,67.1],[66.9,48.6],[66.9,67.5],[67.1,70.4],[67.1,71.2],[67.3,64],[67.4,64.7],[67.5,46.8],[67.5,64.6],[67.5,72.9],[67.7,44.4],[67.7,45.2],[67.9,50.7],[67.9,65.8],[67.9,92.6],[68.1,30.4],[68.1,30.5],[68.1,45.8],[68.2,50.1],[68.4,63.2],[68.5,11.1],[68.6,40.2],[68.6,46.4],[68.6,46.5],[68.7,63.6],[68.8,37.6],[68.8,63.3],[69,54.8],[69.2,39.3],[69.3,69.7],[69.4,16.4],[69.4,16.5],[69.4,57.7],[69.5,80.1],[69.7,37.8],[69.8,70.4],[69.9,39.6],[69.9,70.7],[70.1,68.5],[70.2,39.3],[70.5,41.8],[70.6,44.9],[70.7,17.2],[70.7,69],[70.8,43.4],[70.8,64.5],[71,67.4],[71,67.5],[71,70.7],[71.3,45.5],[71.3,63.9],[71.5,64.5],[71.8,38.8],[72.1,63],[72.1,68.8],[72.3,17.4],[72.5,45.1],[72.5,69.7],[72.5,70.8],[72.6,52.7],[73.2,58.1],[73.7,56.7],[73.8,38],[74.1,39.6],[74.2,60.4],[74.2,60.5],[74.3,52.4],[74.3,52.5],[74.6,62.1],[74.6,63.8],[74.9,65.3],[75.4,42.9],[75.5,42.9],[75.7,38.4],[75.7,38.5],[75.9,66.2],[76,58.2],[76.3,68.6],[76.3,78.5],[76.4,70.7],[76.5,65],[76.5,70.8],[76.6,45.4],[76.6,45.5],[76.6,60.8],[76.8,48],[76.9,14.7],[77,49.4],[77,66],[77.2,49.9],[77.4,64.3],[77.5,63.8],[77.6,52.4],[77.6,52.5],[77.6,56.3],[77.7,64.7],[77.7,66.2],[77.8,48.7],[77.8,49.8],[78,41.5],[78.2,70.4],[78.2,70.5],[78.8,63.5],[78.9,63.4],[79.3,69.7],[79.6,62.4],[79.6,63.1],[80.3,59.3],[81.2,67.2],[81.6,64],[82.1,70.3],[82.3,68.7],[83.3,70.8],[84.6,70.9]] }
}
,2817: {
0: { count: 9, coords: [[23.8,19.9],[25,20.9],[26.6,17.6],[29.8,25.8],[32.4,28.9],[32.4,57.6],[32.5,28.9],[39.1,32.9],[40.7,33]] }
}
,3711: {
0: { count: 345, coords: [[20.4,68.6],[20.4,76.6],[20.5,63.5],[20.5,68.6],[20.6,70.3],[20.6,75.6],[20.7,67.7],[20.8,56.1],[20.8,59.1],[20.8,64.6],[20.8,72.3],[20.8,78.7],[21,74.4],[21,74.5],[21.1,62.5],[21.2,52.9],[21.2,62.4],[21.4,77.1],[22.2,53.2],[22.3,82.2],[22.4,84],[22.5,84],[22.6,78.4],[22.6,78.5],[22.8,51],[22.9,49.5],[23,49.4],[23.3,48.4],[23.7,85.8],[23.9,47.3],[23.9,76.9],[25,46.2],[26.4,43.7],[26.5,43.7],[26.7,84],[27.8,85.3],[28,41.2],[28,63],[28.5,42.8],[28.7,61.8],[29,86.1],[29.7,38.4],[29.7,38.5],[30.1,53.4],[30.2,53.5],[30.3,62],[30.4,37.4],[30.5,37.4],[30.5,55.3],[30.5,93.4],[30.9,50.8],[31.1,36.4],[31.9,55.3],[32,63.2],[32.4,36.3],[32.9,32.8],[33.4,46.6],[33.5,46.6],[33.5,76],[33.6,45.8],[33.7,32.3],[33.7,35.4],[33.7,35.5],[33.7,55.8],[33.8,40.5],[33.8,73.4],[33.9,49],[34,38.6],[34.2,78.3],[34.3,48],[34.3,71.8],[34.4,58],[34.5,58],[34.5,88.9],[34.6,49.5],[34.9,55.9],[35.1,59.4],[35.1,59.5],[35.2,38.8],[35.2,54.7],[35.2,66.9],[35.3,36.4],[35.3,36.5],[35.3,45.4],[35.3,45.5],[35.3,46.8],[35.3,92.5],[35.4,37.5],[35.4,85.2],[35.6,70.7],[35.7,79.2],[35.8,40.1],[35.8,49.9],[35.8,67.6],[35.8,88.1],[37.4,85.6],[37.4,87.6],[38.1,23.7],[38.1,54.9],[38.1,88.4],[38.1,88.5],[38.3,47.1],[38.4,38.6],[38.5,69.3],[38.6,52.7],[38.6,88.2],[38.7,36.8],[38.9,23.4],[38.9,23.5],[38.9,74.1],[39.2,20.3],[39.2,72],[39.3,75.4],[39.3,75.5],[39.6,32.8],[39.9,39.4],[39.9,39.5],[40,88.4],[40,88.5],[40.1,50.9],[40.3,37],[40.5,63.3],[40.7,72],[40.7,74.1],[40.8,20.9],[40.8,70.1],[40.9,91.5],[41.1,33.9],[41.2,88],[42,75.8],[42.4,87.9],[42.5,88],[43.1,37.3],[43.1,37.6],[43.6,76.5],[43.9,88.1],[44.1,23.1],[44.1,46.8],[44.2,21.3],[44.8,88.6],[45.8,21.3],[45.8,26.4],[45.8,26.6],[45.8,44.1],[45.9,87.7],[46,60.9],[46.2,63],[46.2,65.5],[46.3,65.4],[46.5,88.4],[46.5,88.5],[46.8,39.8],[46.8,80.2],[47.2,73],[47.3,20.4],[47.3,41.5],[47.4,36],[47.4,66.8],[47.5,41.7],[47.8,43.2],[47.8,87.8],[47.9,30],[48,33.2],[48.1,22.4],[48.1,22.5],[48.3,66.4],[48.3,70.9],[48.4,66.5],[48.4,76.8],[48.5,76.8],[48.5,88.3],[49,42.6],[49.3,32.9],[49.9,41.9],[49.9,88.5],[50,65.3],[50.3,77],[50.4,32.9],[50.7,63],[50.7,87.9],[51.3,63.5],[51.3,78.2],[51.4,21.7],[51.4,62.2],[51.5,62.1],[51.5,82.1],[51.6,60.4],[51.6,60.6],[51.6,63.1],[52,88.1],[52.2,34.4],[52.2,34.5],[52.5,75.2],[52.7,37.4],[52.7,39.9],[52.7,87.2],[52.9,22.7],[53.2,21],[53.3,38.4],[53.3,38.5],[53.3,62.2],[53.4,87.6],[53.5,87.7],[53.8,41],[54.1,83.5],[54.2,83.4],[54.3,46.1],[54.3,92.5],[54.9,24],[55,68.4],[55.1,44.1],[55.2,76.7],[55.3,63.3],[55.3,88.4],[55.3,88.5],[55.4,21.2],[55.4,66.4],[55.4,66.5],[55.5,21.2],[55.5,66.4],[55.7,60.4],[55.7,60.6],[55.8,81.3],[56,87.4],[56.1,87.5],[56.5,57.1],[56.6,88],[56.7,50.4],[56.9,21.5],[57,21.4],[57.1,23.3],[57.1,68.1],[57.2,66.8],[57.2,92.7],[57.6,98.1],[57.7,49.1],[57.8,76.8],[57.9,67.3],[57.9,97.1],[58,28.9],[58,58.1],[58.1,43.5],[58.2,43.4],[58.4,55.7],[58.5,33.4],[58.7,64.4],[58.7,64.5],[58.8,21],[58.8,88.7],[58.9,27.8],[58.9,35.7],[58.9,54],[58.9,57.4],[59,86.9],[59.2,29.6],[59.2,48.8],[59.3,60],[59.4,37.2],[59.4,75.4],[59.4,75.5],[59.5,75.4],[59.5,75.5],[59.7,81.3],[59.8,37.2],[59.8,57.6],[59.9,82.8],[60,60.2],[60.2,84.9],[60.3,67.3],[60.3,88.8],[60.4,27.2],[62.1,69.5],[62.2,34.7],[62.2,74.3],[62.7,70.8],[62.8,29.4],[62.8,62.3],[62.9,29.5],[63,67.1],[63.1,78.9],[63.2,76.8],[63.5,44.4],[63.7,34.8],[64,83.5],[64.2,53.5],[64.3,53.4],[64.4,55.7],[64.5,55.7],[64.6,45.4],[64.6,45.5],[64.6,91.8],[64.8,37.7],[65.1,30.4],[65.1,30.5],[65.2,63.3],[65.2,82.4],[65.2,82.5],[65.4,41.5],[65.5,41.4],[66.2,33.3],[66.2,33.5],[66.2,55.2],[66.4,45.1],[66.5,45],[66.5,90.3],[66.7,40.4],[66.7,49.1],[66.9,81.9],[67.3,63],[67.5,76.5],[67.7,56.3],[68,78.8],[68.2,33.7],[68.3,45.4],[68.3,45.5],[68.6,53.9],[69.4,58.3],[69.5,58.3],[69.8,54.6],[69.9,74.7],[70.3,39.1],[70.8,55.2],[70.9,60.8],[71.3,63.8],[71.9,72.8],[72.5,54.4],[72.6,54.5],[73.6,59.8],[74,69.2],[74.2,56.1],[74.3,48.5],[74.6,58.2],[74.8,59.8],[75.1,54],[75.4,40.9],[75.5,41],[75.9,50.2],[76.9,61.3],[77.8,55.2],[77.9,63.8],[78.4,58],[78.5,62.2],[78.6,52.4],[78.6,52.5],[78.7,55]] }
}
,4197: {
0: { count: 176, coords: [[6.1,66.1],[7.2,65.9],[8,52.8],[9.9,65.9],[11.7,65],[14,43.3],[14,43.5],[15.2,43.6],[15.3,43.4],[16.3,38.1],[16.4,76.9],[17.9,66.8],[18.4,71],[19.2,67.7],[20.7,62.5],[20.8,58.4],[20.8,58.5],[20.8,62.4],[21,59.9],[21.4,48.3],[21.4,99],[21.5,48.4],[21.9,68.1],[22.7,46.4],[22.8,62.7],[23,91.4],[23.1,45.3],[23.3,47.3],[25.4,24.8],[25.4,59.8],[25.5,24.7],[25.5,59.8],[25.7,61.4],[26.6,22.2],[27.2,61.2],[28.1,62],[28.4,19.9],[28.4,40.7],[28.5,19.8],[28.5,41.6],[29.6,56.4],[29.7,17.2],[29.7,38.3],[29.7,38.5],[29.8,55.2],[30,43.9],[30.2,40.7],[30.9,46.1],[31.4,48.2],[31.5,48.2],[31.6,97.8],[33.4,48.6],[33.5,48.7],[33.9,13.8],[33.9,54.3],[34,29.3],[34.8,12.4],[34.8,12.5],[35.2,27.1],[35.3,13.5],[35.7,12.6],[38.6,59.4],[38.7,59.5],[38.8,18.5],[38.9,18.4],[39.3,53.2],[39.6,9.8],[41,23.4],[41,23.5],[41.3,17.4],[41.3,17.5],[41.3,37.3],[41.9,25.3],[41.9,25.5],[41.9,52.5],[43.6,58.6],[43.8,54.9],[45,60.8],[45.1,30.2],[45.1,62.5],[46.5,71.3],[46.6,35.4],[46.6,35.5],[48.6,35.6],[49.2,87.2],[49.3,43.4],[49.3,43.5],[49.3,89.7],[49.4,45.3],[49.4,45.5],[49.4,91.7],[49.5,45.4],[49.5,45.6],[49.6,48],[50,49.4],[50.1,49.6],[50.2,58.3],[51.7,36.9],[51.7,77.8],[51.8,82.7],[53.2,55.5],[53.3,55.4],[53.5,31.5],[53.6,31.3],[54.9,30.8],[56.3,56.8],[56.4,54.7],[56.6,54.8],[57.8,80.2],[57.9,25.7],[58.5,17.1],[58.5,36.1],[58.7,20.9],[58.7,84.1],[58.7,85.9],[59.8,17.1],[59.8,37.4],[60.3,52],[60.5,52],[60.7,57.9],[60.8,56.4],[60.8,56.6],[61.9,17.4],[61.9,17.5],[61.9,36],[62.2,16.4],[62.9,52.9],[63.9,69.2],[64.3,72.3],[64.4,41.6],[64.5,41.6],[64.6,41.4],[64.8,54],[65,77],[66.4,51.4],[66.4,51.8],[66.5,51.4],[66.5,51.8],[67.3,20.2],[67.8,57],[68.2,72.7],[69.2,56.4],[69.2,56.7],[70.6,25],[72,21.3],[72.1,43.9],[72.4,41.4],[72.4,41.5],[72.5,41.6],[72.6,27.4],[72.6,27.5],[72.8,49.6],[73.4,57.1],[73.5,57],[74.3,45],[74.4,90.7],[75.9,58.2],[77.2,87.8],[78,67.3],[78,67.5],[78,69.9],[78.1,59.3],[78.3,86.2],[79,66.3],[79.1,67.2],[79.1,68.3],[79.2,57.3],[79.3,78.4],[79.3,78.5],[79.3,81.2],[82.6,41.9],[83.7,43.3],[83.7,88.3],[84.8,65.1],[86.1,76],[87,68]] }
}
,4264: {
1: { count: 1, coords: [[31.1,69.7]] }
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Blaze

  • x86
  • Hero Member
  • *****
  • Posts: 7136
  • Canadian
    • View Profile
    • Maide
Re: [PHP] Organizing an Array
« Reply #3 on: April 12, 2010, 12:10:32 pm »
Should label this 'Organizing a JSON array'.
And like a fool I believed myself, and thought I was somebody else...

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing an Array
« Reply #4 on: April 12, 2010, 01:22:17 pm »
@Joe: did you know PHP has native support for parsing JSON?

http://us3.php.net/json_decode

Hopefully you're using PHP >= 5.2.0.  I think the extension is usually installed by default.

@rabbit: why are you such an intolerant dick?  Seriously, all of your comments were pedantic at best, and most were based on coding style preference that are functionally irrelevant.

printf is still useful.  I think it makes code much more readable if it's used properly.  For example:

Code: [Select]
printf('<form action="%s" method="POST">', $_SERVER['PHP_SELF']);

Of course this is easy to abuse and it can end up making code harder to follow, but if you're going to print out HTML like this, I'd argue that printf is the way to do it.

I use sprintf in my PHP pretty extensively because I think it makes it a lot clearer what's going to happen.

I always try to avoid echoing output.  My usual pattern is to store relevant information in a well-formatted structure, and just write HTML with <?= ?> where needed.

This is a bit of a matter of preference, but I've found it makes things quite a bit easier to deal when when projects get big.

Offline Joe

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [PHP] Organizing a JSON Array
« Reply #5 on: April 12, 2010, 02:06:38 pm »
That's pretty neat. Basically, this was for organizing data on Wowhead into a script for a bot. :P
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #6 on: April 12, 2010, 02:17:40 pm »
That's pretty neat. Basically, this was for organizing data on Wowhead into a script for a bot. :P

Ah, okay.  Yeah, if you're pulling from their AJAX sources, they're likely JSON, so it's ridiculously easy to handle.

Also, you could probably use CURL to fetch a page instead of pasting the source in.  Check this out:

http://ulkesh.sidoh.org/public

Thrown together in 5 minutes with jquery and a CURL wrapper.  Of course you wouldn't need to do AJAX if you're going to submit the form and parse it.  Just submit the URL, grab the page, parse it, and print out what you're already printing out.

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing an Array
« Reply #7 on: April 12, 2010, 07:15:49 pm »
@rabbit: why are you such an intolerant dick?  Seriously, all of your comments were pedantic at best, and most were based on coding style preference that are functionally irrelevant.
Nope.  Double quoted strings get passed through an additional parser which checks for complex syntax, so when not using complex syntax it's better to use single quoted strings.  Passing double quoted strings (without complex syntax) to printf (without any other arguments) is also less efficient than just using print with single quoted strings.  And how are the following "pedantic at best"?
Quote
4. If you don't check preg_match_all result you might end up with null.
5. I just tested your code, and all it prints out is:
Checking results of a function before using them and assuming they will work is one of the first things any course, book, or tutorial will teach.  Also, I copy and pasted his code and used a short string ("[1, 1] [2, 2] [3, 3], [4, 4]" IIRC) and I said what it printed out, which means something wasn't working properly.

I'd really like to know why you insulted me and my comments, especially when I was making specific comments which would apply to any situation and with any dataset of the general form "[value, value, ...]" (and when Joe didn't provide a data set in the first place).  Your remarks were disrespectful and unappreciated.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing an Array
« Reply #8 on: April 12, 2010, 09:33:24 pm »
Double quoted strings get passed through an additional parser which checks for complex syntax, so when not using complex syntax it's better to use single quoted strings.  Passing double quoted strings (without complex syntax) to printf (without any other arguments) is also less efficient than just using print with single quoted strings.

There are af few obvious things that should be said:

1. I'm aware, and I'd imagine Joe is too.  The differences are only relevant if you're concerned about minute improvements.  Since this is just something of a personal convenience for him, there's no reason to care.  It doesn't make any sort of noticeable difference.
2. If you were actually concerned about efficiency, you wouldn't be using regular expressions...
3. Even if I grant you your silly corrections, it doesn't excuse the hostile way you presented them.

4. If you don't check preg_match_all result you might end up with null.

It's pedantic because his code serves as a tool for him to use.  It's not production code where you're sanitizing input and checking for valid format.  It doesn't matter.

5. I just tested your code, and all it prints out is:
Checking results of a function before using them and assuming they will work is one of the first things any course, book, or tutorial will teach.  Also, I copy and pasted his code and used a short string ("[1, 1] [2, 2] [3, 3], [4, 4]" IIRC) and I said what it printed out, which means something wasn't working properly.

Again, it doesn't matter if he's using it as a personal tool.  The first useful thing you learn is only implement something if it's worthwhile.  Error checking something like this is not worthwhile... if this was something he wrote that was meant to be used by the world, then I'd be more understanding.  However, you're just looking for things to criticize and being a dick instead of providing useful input.

I got the impression that it was because you weren't following the expected input syntax.

I'd really like to know why you insulted me and my comments, especially when I was making specific comments which would apply to any situation and with any dataset of the general form "[value, value, ...]" (and when Joe didn't provide a data set in the first place).  Your remarks were disrespectful and unappreciated.

I think I may have gotten the wrong impression of your post, and if that's the case, I apologize.  You seemed to look for problems in Joe's code instead of making worthwhile suggestions.  I'm aware that there can be some efficiency concerns with using double quotes, printf, etc., but none of them are relevant in this case.  It might be "good practice," but "good practice" comments are only helpful if you provide the motivation, and at least admit that it doesn't make a difference in this case.

I'm sorry for overreacting, but it makes my blood boil when I see someone being criticized for things that don't matter.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #9 on: April 12, 2010, 09:40:34 pm »
Joe, here is the aforementioned CURL wrapper.  I also provided an extension that caches the result for a fixed number of seconds.

http://ulkesh.sidoh.org/public/CURLClient.phps
http://ulkesh.sidoh.org/public/CURLCacheClient.phps

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #10 on: April 12, 2010, 11:12:40 pm »
I commented on things that popped to my attention in the order they did.  I didn't say "you're dumb for using printf" or "lol ur code sux".  I wasn't being insulting.  All I did was try to point out things that could be improved.

And replacing 3 lines of code, 1 including a regex function, with 1 line of code consisting of only 1 regex function, the net efficiency still increases.

It is is a production tool; he clearly stated he planned to use it for "purposes..." which he later clarified as "a bot".

And sanitization and checking should always be done, even if you remove them later once you're confident in it, and they should especially be present if you're going to release the snippet as something useful for others, as the other people might not know enough to figure out why it isn't working.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #11 on: April 12, 2010, 11:34:48 pm »
I commented on things that popped to my attention in the order they did.  I didn't say "you're dumb for using printf" or "lol ur code sux".  I wasn't being insulting.  All I did was try to point out things that could be improved.

I guess I got a little annoyed because it seemed like you were being ridiculously pedantic.  I still contest that most of your suggestions weren't very insightful, and if he were to make the changes, it wouldn't make any sort of noticeable difference.

And replacing 3 lines of code, 1 including a regex function, with 1 line of code consisting of only 1 regex function, the net efficiency still increases.

I'm saying that if you're concerned with things in the neighborhood of microseconds (as you would need to be to care about the difference in efficiency with print vs. printf, '' vs ""), then you'd never use regular expressions at all.

It is is a production tool; he clearly stated he planned to use it for "purposes..." which he later clarified as "a bot".

I got the impression that he was taking the data from WoWhead and putting it into a format he could use, not that the bot would make direct use of the script.  Even so, the difference in efficiency is completely negligible in this case.

And sanitization and checking should always be done, even if you remove them later once you're confident in it, and they should especially be present if you're going to release the snippet as something useful for others, as the other people might not know enough to figure out why it isn't working.

I completely disagree.  Sanitization and error checking should not be done if it's a waste of time.

I'm pretty sure you'd be able to tell what's going wrong given the output of the program if you don't do any explicit error handling.  If it was a harsher language, it might be nice to have some sanity checks in place, but here, I don't see any benefit.

Sure, it wouldn't hurt, but honestly, I don't think it'd help much either.  If someone can't figure out what's going wrong if there are no matches for the regular expression, then they're pretty lost overall...

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #12 on: April 13, 2010, 02:17:33 am »
You'd rather use a whole bunch of string manipulation and checks than regex (which is the same thing done for you)?  Again, doesn't make much sense.  I don't know anything about WoW, but it sounded to me like this script would be called quite often to get new values of....stuff.

And there ARE proper ways to do things, and it's important to stick with it when you're in the early stages of a language.

Offline Sidoh

  • Moderator
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [PHP] Organizing a JSON Array
« Reply #13 on: April 13, 2010, 02:31:33 am »
You'd rather use a whole bunch of string manipulation and checks than regex (which is the same thing done for you)?  Again, doesn't make much sense.  I don't know anything about WoW, but it sounded to me like this script would be called quite often to get new values of....stuff.

No, I'd rather use json_decode, but that's beside the point.  If you're seriously concerned about efficiency, then using regular expressions is much more of a concern than using printf instead of print or "" instead ''.

Regular expressions are much cleaner and easier, but they're almost never the most efficient solution.  If I had to choose between writing my own parser for JSON and using regular expressions to parse a subset of JSON, of course I'd choose the regular expressions route (as long as I'm confined to this or a similar problem).  That's not what I said, though.  You seem to be all uppity about minutely more efficient code, so it just seemed a rather severe double-standard to go and use regular expressions after a mild tirade against using printf.

And there ARE proper ways to do things, and it's important to stick with it when you're in the early stages of a language.

Oh common.  I hope you realize how arbitrary "proper" is.  Is it more important that you're using the most efficient code possible, or is it okay if the code is negligibly less efficient, but far more readable and maintainable?  As long as you're not writing drivers, I'd argue that the readability and maintainability trump efficiency as long as the code you're comparing is only marginally less efficient.

I concede that there's no reason to use printf instead of print if you're not going to take advantage of the superset of features printf offers, but there's still not going to be a noticeable difference in performance...

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [PHP] Organizing a JSON Array
« Reply #14 on: April 13, 2010, 07:43:52 pm »
Fine, print vs printf aside, in his very first post he said it could be easily adapted to any kind of array, so assuming the input will be JSON formatted is bad.

And as for readability, comments exist for a reason, although they should be used to explain why, not how (I'll give you that one).  Additionally, changing " to ' boosts performance and doesn't mangle readability like you seem to be implying.