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).
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);
Help parsing JSON array using PHP
Getting contents of a JSON array into PHP as part of a $_POST array
update json array in mysql not working php
[HELP] json_decode on PHP fails to convert an array of JSON objects
Videos
I have figured out half of what I am trying to do here.
I have a JSON array that is being returned via API. I am wanting to parse that array and pull out just the Unique ID and Email Address for each user.
Each user has a key called 'unique_id' regardless of whether or not it is defined. Each user only has a key called 'email' if a user in fact has an email address defined.
Sample Code:
foreach ($arr["data"]["users"] as $value) {
echo $value["unique_id"];
echo $value["email"];
}This code works....sort of. This will output the unique_id for each user. If there is no value defined, it simply returns a blank value. Cool. However, for the key email, I get an error: Undefined array key "email".
Am I getting this because not every user has this key?
You can see a sample of the array output here: https://pastebin.com/9D4mWyKL