[ create a new paste ] login | about

Link: http://codepad.org/N4mevrZG    [ raw code | output | fork ]

PHP, pasted on Aug 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function jsonify_chunkies($string) {
  $out = array();
  $lines = preg_split('/\r\n|\r|\n/', $string);
  
  foreach ($lines as $i => $line) {
    $out[] = '"' . $i . '": ' . $line . "";
  }
  
  return "{\n" . join(",\n", $out) . "\n}";
}

$server_response = '[ "1", "Apples", "Fruit" ]' . "\n" .
'[ "1", "Banannas", "Fruit" ]' . "\n" .
'[ "1", "Pears", "Fruit" ]' . "\n" .
'[ "1", "Potatoes", "Veg" ]';
echo jsonify_chunkies($server_response);


Output:
1
2
3
4
5
6
{
"0": [ "1", "Apples", "Fruit" ],
"1": [ "1", "Banannas", "Fruit" ],
"2": [ "1", "Pears", "Fruit" ],
"3": [ "1", "Potatoes", "Veg" ]
}


Create a new paste based on this one


Comments: