JSON - PHP


Array to Json

// Non-consecutive number keys are OK for PHP
// but not for a JavaScript array
$array = array(
  2 => array("Afghanistan",32,13),
  4 => array("Albania",32,12)
);
// array_values() removes the original keys and replaces
// with plain consecutive numbers
$out = array_values($array);
json_encode($out);
// [["Afghanistan",32,13],["Albania",32,12]]


Json to Array

$json_data = "["2":["Afghanistan",32,12],"4":["Albania",32,12]]";$out = json_decode($json_data, true);

댓글

이 블로그의 인기 게시물