I have only worked with JSON once before, but I don't recall how to use it with PHP.
If I have a script that returns JSON like this:
{
"bunisess":[
"business",
"bonuses",
"burnooses",
"boniness",
"burnoose's"
]
}
how can I take that and make each value a value in a PHP array. The keys just being numbers from 0 onwards?
Use json_decode, but pass true as the second parameter to get an associative array back:
$json='{"bunisess":["business","bonuses","burnooses","boniness","burnoose\'s"]}';
$data=json_decode($json, true);
Now, you can use array_values to get a numerically indexed array as you required:
$data=array_values($data);
A simple google search could have lead you to this page : http://php.net/manual/fr/function.json-decode.php
The subarray will be respecting exatcly what you're asking for.
Assuming that $data is the complete JSON string
$stdObject = json_decode($data, true);
print_r($array);
You should get an array with one key bunisess and value another numeric array with the other values.
Related
I have an array of 'servers' that I'm storing in a JSON file.
The JSON file looks like this:
{"1":{"available":1,"players":0,"maxplayers":4}}
I retrieve this value with this:
$servers = (array) json_decode(file_get_contents("activeservers.json"));
However, when I try to access the array with $server = $servers[$id], $server is null. I noticed that the key is in quotes, so I also tried casting $id to a string, and surrounding it with quotes (") which didn't work.
Something to note, is that this code is returning "NULL":
foreach(array_keys($servers) as $key){
var_dump($servers[$key]);
}
Your code is wrong. Also you don't need to type cast when doing a json_decode, you can instead set the second parameter to true more info here.
Also you don't need to use the array_keys function in your foreach loop,
try this.
$json = '{"1":{"available":1,"players":0,"maxplayers":4}}';
$servers = json_decode($json, true);
foreach($servers as $key => $value) {
print $value["available"];
}
Do a print_r($value) to get all the array keys available to use. Also you could take advantage of the $key variable to print out the array key of the parent array.
Thanks, #Rizier123 (who solved the question).
Apparently passing TRUE as the second parameter to my json_decode function fixes the issue.
After checking the PHP documentation for json_decode() (PHP: json_decode), it seems that passing this parameter means that the resulting decoded array is automatically converted into an associative array (and this is recurring, meaning that this automatically happens for sub-arrays).
Edit: #Rizier123 also says that "you might want to read: stackoverflow.com/a/10333200 to understand a bit better why it is so "weird" and your method didn't work properly."
I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}
I was trying to get some values from MySQL database and when converting into JSON array using json_encode() I got a JSON object , after a while I found out that the indices was the root cause of the problem
here's the example I mentioned
<?php
$array = array(0=>"zero",2=>"two");
$another_array=array(0=>"zero",1=>"one");
print_r(json_encode($array)); // output: {"0":"zero","2":"two"}
print_r(json_encode($another_array)); //output: ["zero","one"]
?>
so what's the reason for that ?
Because array(0=>"zero",1=>"one") is the same as array("zero", "one") and the JSON encoder assumes the latter is a list (since that's how PHP does lists) and has no way to tell the former from the latter.
If you want to force json_encode to treat all arrays as objects, pass JSON_FORCE_OBJECT as the second argument:
json_encode(array(0=>"zero",1=>"one"), JSON_FORCE_OBJECT)
// {"0":"zero","1":"one"}
If you always want a json list instead, get rid of the keys (e.g. using array_values()) before encoding.
I my php file, I need to make my own Json array.
for($i=1;$i<$arraySize+1;$i++){
$idJson[$i]=$i.":".$birdIDArray[$i-1];
}
for($i=$arraySize+1 ;$i<$arraySize*2+1; $i++){
$idJson[$i]=$i.":".$rankArray[$i-($arraySize+1)];
}
When I use
print(json_encode($idJson));
the OUTPUT : ["0:3","1:15","2:3","3:14","4:1","5:2","6:2"]
But i need the output like this ["0":"3","1":"15","2":"3","3":"14","4":"1","5":2","6":"2"]
When I going to add " mark
for($i=1;$i<$arraySize+1;$i++){
$idJson[$i]=$i.'"'.":".'"'.$birdIDArray[$i-1];
}
for($i=$arraySize+1 ;$i<$arraySize*2+1; $i++){
$idJson[$i]=$i.'"'.":".'"'.$rankArray[$i-($arraySize+1)];
}
It prints like this
["0:3","1\":\"15","2\":\"3","3\":\"14","4\":\"1","5\":\"2","6\":\"2"]
How can I avoid from printing this \ sign?
I'm assuming you want a JSON object like this:
{"0":"3", ... }
The problem here is that Javascript/JSON distinguishes between key-value pairs, which are objects, and numerically indexed lists, which are arrays, while PHP uses arrays for both these things. With json_encode it depends on whether the PHP array is a continuously numerically indexed array, in which case you get a JSON array, or anything else, in which case you get a JSON object.
What you want is to force a JSON object even for a continuously numerically indexed array. The first question here would be: why?! But if you're really sure you want that (again, why?!), there's the JSON_FORCE_OBJECT flag in PHP 5.3+:
echo json_encode(array("3", "15", "3"), JSON_FORCE_OBJECT);
// {"0":"3","1":"15","2":"3"}
But I'll say again that that's pretty pointless. If you use a regular array like ["3","15","3"], the keys to those elements are already implicitly 0, 1 and 2. There's no need to enforce them as object keys.
I have a json record like blow
[{"low":null,"high":10,"type":2,"value":0},{"low":10,"high":60,"type":1,"value":10},{"low":60,"high":null,"type":2,"value":11}]
I would like to create array using this. i tried json_decode its not help i just want create a associative array from this json
any help
thank you
Make sure you're passing true to the second parameter in json_decode(), which specifies that you want an associative array returned.
$arr = json_decode($string, true);
Parameters
assoc
When TRUE, returned objects will be converted into associative arrays.
see this page : http://php.net/manual/en/function.json-decode.php
use json_decode with this format : json_decode($value,true);
your code : http://pastebin.com/4vdVHjQN