Easy peasy lemon squeezy: http://www.php.net/manual/en/function.json-encode.php
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
There's a post by andyrusterholz at g-m-a-i-l dot c-o-m on the aforementioned page that can also handle complex nested arrays (if that's your thing).
php - How to convert JSON string to array - Stack Overflow
Help converting PHP arrays to JSON format
PHP Array to Json Object - Stack Overflow
What is the appropriate way to encode an array into JSON?
Easy peasy lemon squeezy: http://www.php.net/manual/en/function.json-encode.php
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
There's a post by andyrusterholz at g-m-a-i-l dot c-o-m on the aforementioned page that can also handle complex nested arrays (if that's your thing).
Use PHP's native json_encode, like this:
<?php
$arr = array(
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
),
array(
"region" => "valore",
"price" => "valore2"
)
);
echo json_encode($arr);
?>
Update: To answer your question in the comment. You do it like this:
$named_array = array(
"nome_array" => array(
array(
"foo" => "bar"
),
array(
"foo" => "baz"
)
)
);
echo json_encode($named_array);
If you pass the JSON in your post to json_decode, it will fail. Valid JSON strings have quoted keys:
json_decode('{foo:"bar"}'); // this fails
json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")
json_decode('{"foo":"bar"}'); // returns an object, not an array.
Try this:
$data = json_decode($your_json_string, TRUE);
the second parameter will make decoded json string into an associative arrays.
As stated above in the links, if you store your PDO data into an array using FETCH_ASSOC, you can then use json_encode to convert. Typically, I have a processing file to handle this type of request (all CRUD actually), that simply takes the array and echoes json_encode($array) to the calling query. Using the echo method allows me to append an identifier to the beginning of the json data for use in my ajax/jquery scripts.
Hi there. Iโm struggling to get my head around converting arrays into JSON format. I have some code that queries my database and successfully returns 2 columns (contact_type and contact_det) into an array:
array(3) (
[0] => array(4) (
[contact_type] => (string) home_tel
[0] => (string) home_tel
[contact_det] => (string) 02072726347
[1] => (string) 02072726347
)
[1] => array(4) (
[contact_type] => (string) personal_mobile
[0] => (string) personal_mobile
[contact_det] => (string) 01977591328
[1] => (string) 01977591328
)
[2] => array(4) (
[contact_type] => (string) work_tel
[0] => (string) work_tel
[contact_det] => (string) 01977591328
[1] => (string) 019775913...
Can anybody tell me the best/easiest way to convert the above array data into JSON format, please?
Many thanks
You want to json_encode($json, JSON_FORCE_OBJECT).
The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.
You can also eliminate the use of array_push for some slightly cleaner code:
$json[] = ['ip' => $ip, 'port' => $port];
just use only
$response=array();
$response["0"]=array("ip" => "192.168.0.1",
"port" => "2016");
$json=json_encode($response,JSON_FORCE_OBJECT);