Convert js stringified string to multidimensional array in php [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I get this kind of string from JS to PHP.
[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]
How can I convert it to a php multidimensional array?
Note:
I tried json_encode without getting the desired result.

<?php
$string = '[[1,"10.00","10.00","A"],[2,"20.00","25.00","B"],[3,"10.00","25.00","C"],[6,"10.00","25.00","D"]]';
$result = json_decode($string);
var_dump($result);
?>
You should use json_decode instead of json_encode for convert JSON to array PHP
Result:

Related

proper syntax for accessing values from dictionary [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
php parsing json data
(4 answers)
Closed 1 year ago.
I have the following code that generates a dictionary of key/value pairs in php:
$json = file_get_contents('php://input');
$obj = json_decode($json);
print_r(json_encode($obj));
print_r produces the following output:
{"email":"someemail#gmail.com","password":"compas"}
how do you output just the value of the email key? Why doesn't $obj["email"] work?
To turn the json_decode to an arry use following form see manual
$myjson = '{"email":"someemail#gmail.com","password":"compas"}';
$obj = json_decode($myjson,true);
print_r($obj[email]);
returns someemail#gmail.com

Separate the key from the value [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
I set up a new site and want a workaround for the problem.
I have this text in the mysql table
{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}
I want to fetch the ID and its value by php
For example
$id[200] = 5
You can use json_decode() to convert JSON string into an array. Since your JSON string contains numeric indexes so you need to use curly brackets to fetch the values
$str = '{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}';
$array = json_decode($str);
echo $array->{'200'};
Or you can add second parameter in json_decode as true to fetch the value as normal array
$array = json_decode($str,true);
echo $array[200];
Here is the live demo

JSON array convert to PHP Array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have a json object array. I want to read the this value. how to read value using php.
My json object Array
[{"employeeId":"ioc-nugegoda","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nugeqqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nusqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"}]
$jsonObject = json_decode('{"employeeId":"ioc-nugegoda","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nugeqqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nusqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"}');
Which you can find here. After that you can access all values of the json object by its key:
echo $jsonObject["employeeId"];
//Should return: ioc-nugegoda
If you have an array arround your JsonObject you need to access the object itself first.. this would look like:
echo $jsonObject[0]["employeeId"];

json to array in php index error [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have a json text like this:
$text ='{"id":"12","count":"1","1":{"gkey":"g_c5218"},"0":{"gkey":"g_4b4c6"}}';
I want convert it to array:
$arr = json_encode($text,true);
Now I want call $arr['id'] But I get index error.
what is my wrong?
json_encode() - change an array to the json
json_decode() - change json to the array/object
You should use json_decode.

parse multilevel json in php [duplicate]

This question already has answers here:
Trying to parse JSON with PHP
(2 answers)
Accessing JSON array after json_decode/multidimensional array
(3 answers)
Closed 8 years ago.
I am receiving a multilevel json like this:
{"id":"3",
"field2":"ssss",
"field3":[
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
{"field31":"wwwww",
"field32":"qqqq"},
]}
I am reading json values (first level) like this in php:
$json = file_get_contents('php://input');
$json_post = json_decode(utf8_decode($json), true);
$id = $json_post['id'];
But I don't know how to get the json in the second level. What is the simplest way? I just need to get the json in other variable like $json_post, I will read it in a for bucle to get each row of the json file
It's going to give you a PHP object if you run json_decode(utf8_decode($json));
echo $json_post->field3->field31;
EDIT: I missed that you were using the conversion to array. This is what the array would look like
echo $json_post['field3']['field31'];

Categories