Json type array with php how?

Hi everyone…

just need little help if I’m give such string

1447615481,0,0||1052646429,0,0

how to outputingg json type array as u can see below…any shed some light are appreciate…



'[{coor:"1447615481",x:"0",y:"0"},{coor:"1052646429",x:"0",y:"0"}]'


thanks in advanced

Maybe?


<?php
$string = '1447615481,0,0||1052646429,0,0';

$a = new stdClass;
$b = new stdClass;

list($a->coor, $a->x, $a->y, $b->coor, $b->x, $b->y) = sscanf($string, '%d,%d,%d||%d,%d,%d');

echo json_encode(array($a, $b));
/*
  [{"y":0,"x":0,"coor":1447615481},{"y":0,"x":0,"coor":1052646429}]
*/

This should give you the desired result

$coordinates = '1447615481,0,0||1052646429,0,0';
$coordinates = explode('||', $coordinates);
$output = array();

foreach ($coordinates as $coordinate) {
    $temp = explode(',', $coordinate);
    array_push($output, array('coor' => $temp[0], 'x' => $temp[1], 'y' => $temp[2]));
}

echo json_encode($output);

NOTE: The json_encode and json_decode functions only work on PHP 5.2 and higher, if your server doesn’t support this you will need to get the JSON PHP library which can be found at http://www.json.org

Wow…

it do the tricks now…million thanks to AnthonySterling and SgtLegend for the concern…tq again…