How to make json objects within array in php [duplicate] - php

This question already has answers here:
How to create an array for JSON using PHP?
(8 answers)
Closed 8 years ago.
I want to make json as followsin php:
{["key1":"value1"],["key2":"value2"]}
Can any one help?

See the PHP manual for json_encode() and json_decode():
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php

You can do it like this: set it to true, if you want an associative array.
$array = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]',true);
if you want to iterate:
foreach($array as $item) { //foreach element in $arr
$uses = $item['var1']; //etc
}

Related

I want to read all data from Json array in Php codeigniter 4 [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 3 months ago.
I have generated html table data to Json array using jquery, it worked for me and I got the array below, I need php code to iterate each value of this array in php codeigniter 4 controller.
array(1){
[
0
]=>string(966)"[{"ID":"\n2","Name":"\nCP","Price":"\n350.20"},{"ID":"\n3","Name":"\nLFT","Price":"\n700.10"},{"ID":"\n4","Name":"\nRFT","Price":"\n200"},{"ID":"\n5","Name":"\nurinetest","Price":"\n1000"}]"
}
It's a php array that contains a json array, if you are sure you will only have one element in the array, you can do so:
$array = json_decode($var[0]); // $var holds the data you mentioned above
foreach ($array as $item) {
//
}
otherwise:
foreach ($var as $array) { // $var holds the data you mentioned above
$array = json_decode($array);
foreach ($array as $item) {
//
}
}

how to get words after word in string [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I've a json response like this:
'{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}'
above code is in a variable as string
I need just product keys: 123,234;
how can I get product keys from this reponse?
Thanks
Use json_decode to turn it into an associative array.
$jsonString = '{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}';
$json = json_decode($jsonString, true);
$productKeys = [];
foreach($json["data"] as $val)
{
$productKeys[] = $val["productKey"];
}

How to get Json Objects inside the Json Array and store it in MySql In php [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want get Json objects from the json array and store each values in mysql using php.
$arr = "[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]";
try this code
Using json_decode
<?php
$arr = '[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]';
$data = json_decode($arr);
echo "<pre>";
print_r($data);
foreach($data as $k=>$v){
echo $v->fruit." ".$v->isSelected." ".$v->number."<br>"; //store in db here using value
print_r($v);
}
?>

How do I decode this Json array using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
How do I decode this json data using php to get individual values.
{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}
php has a function called json_decode
$json = '{"Alpha":["A","B","C","D","E","F"],"Beta":["1","2","3"],"Gamma": ["a","b","c"]}';
$json_array = json_decode($json,TRUE); // personally I prefer arrays which the TRUE adds
$alpha = $json_array['Alpha'];
$first_alpha_value = $alpha[0];
printf("A == %s\n",$first_alpha_value);

How i can take the json's value in PHP? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 8 years ago.
I would like to know, how take the value "gamertag" from this Json (http://360api.chary.us/?gamertag=superdeder) with PHP.
i would print only the value "superdeder".
thanks a lot.
Andrea.
Use file_get_contents() and convert your JSON to an object with json_decode():
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json);
echo $gamer->Gamertag; // SuperDeder
Or, an array, by passing a truthy value as json_decode()s second parameter:
$gamer = json_decode($json, true);
echo $gamer['Gamertag']; // SuperDeder

Categories