1 Replies - 682 Views - Last Post: 14 August 2013 - 09:36 AM Rate Topic: -----

#1 Whazabazaa   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Sort csv-to-json-output from low to high

Posted 14 August 2013 - 08:07 AM

Hi,

I have a PHP-file which translates a CSV formated txt-file to JSON. The translating-part works well but I have a question: is there a way to sort the JSON-output? I would like to sort the output from low to high (Column ‘C’ from low to high). I have been searching but I can’t find an example which is applicable for me (I want the preg_match).

My PHP-file:
<?php
echo '[';
  
$file  = fopen('testfile.txt', 'r');

$type = '/(' . $_GET["type"]. ')/';

while (($line = fgetcsv($file)) !== FALSE) {  
    list($A, $B, $C) = $line;

    if(preg_match($type, $A)) {
        echo '{"Symbol"="'. $B . '",
		"Number"="'. $C . '"},';
    }
}
echo "]";
?>


´testfile.txt´:
A,B,C
"Character","a",5,
"Character","b",3,
"Character","c",2,
"Character","d",4,
"Character","e",11,
"Character","f",1,
"Character","g",10,
"Character","h",7,
"Character","i",6,
"Character","j",8,
"Character","k",9,
"PunctuationMark","?",13,
"PunctuationMark","!",12,


I get the following output (with ´?type=Character´ added to the URL):
[{"Symbol"="a", "Number"="5"},{"Symbol"="b", "Number"="3"},{"Symbol"="c", "Number"="2"},{"Symbol"="d", "Number"="4"},{"Symbol"="e", "Number"="11"},{"Symbol"="f", "Number"="1"},{"Symbol"="g", "Number"="10"},{"Symbol"="h", "Number"="7"},{"Symbol"="i", "Number"="6"},{"Symbol"="j", "Number"="8"},{"Symbol"="k", "Number"="9"},]


I would like to get my output like this:
[{"Symbol"="f", "Number"="1"},{"Symbol"="c", "Number"="2"},{"Symbol"="b", "Number"="3"},{"Symbol"="d", "Number"="4"},{"Symbol"="a", "Number"="5"},{"Symbol"="i", "Number"="6"},{"Symbol"="h", "Number"="7"},{"Symbol"="j", "Number"="8"},{"Symbol"="k", "Number"="9"},{"Symbol"="g", "Number"="10"},{"Symbol"="e", "Number"="11"},]


Thank you in advance,
Whazabazaa

This post has been edited by Whazabazaa: 14 August 2013 - 08:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sort csv-to-json-output from low to high

#2 JackOfAllTrades   User is online

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: Sort csv-to-json-output from low to high

Posted 14 August 2013 - 09:36 AM

That's not actually JSON you know. If you replace the = with : then yeah, it's JSON.

In any event I wouldn't build the JSON by hand. I'd read into an array of arrays, sort it with a user-supplied function, and then encode to JSON.

My rare freebie:

<?php

function sortByNumberAsc($a, $B)/>
{
  if (intval($a['Number']) < intval($b['Number']))
    return -1;
  else if (intval($a['Number']) > intval($b['Number']))
    return 1;
  return 0;
}

$file  = fopen('testfile.txt', 'r');

$theType = 'Character';
$type = "/($theType)/";

$toEncode = array();
while (($line = fgetcsv($file)) !== FALSE) {
  list($A, $B, $C) = $line;

  if(preg_match($type, $A)) {
    $toEncode[] = array('Symbol' => $B, 'Number' => $C);
  }
}
usort($toEncode, 'sortByNumberAsc');
print(json_encode($toEncode));
?>



php -f whaz.php 
[{"Symbol":"f","Number":"1"},{"Symbol":"c","Number":"2"},{"Symbol":"b","Number":"3"},{"Symbol":"d","Number":"4"},{"Symbol":"a","Number":"5"},{"Symbol":"i","Number":"6"},{"Symbol":"h","Number":"7"},{"Symbol":"j","Number":"8"},{"Symbol":"k","Number":"9"},{"Symbol":"g","Number":"10"},{"Symbol":"e","Number":"11"}]

This post has been edited by JackOfAllTrades: 14 August 2013 - 10:04 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1