Previously I was using a foreach loop to access the data in my JSON object but now I have nested an array inside an array. Here is my JSON
{
"name": "Takeaway Kings",
"menu": [
{
"starter": [
{
"name": "Samosas",
"price": 3.5
},
{
"name": "Chaat",
"price": 1.99
}
]
},
{
"dessert": [
{
"name": "Kulfi",
"price": 2.5
},
{
"name": "Kheer",
"price": 2.99
}
]
},
{
"main": [
{
"name": "Lamb Biryani",
"price": 4.5
},
{
"name": "Chicken Tikka Masala",
"price": 5.99
}
]
}
]
}
I am trying to loop through each array inside menu and then loop through what is in each nested array.
I was previously using this to output data before I changed the JSONObject layout.
<?php foreach($restaurant->menu->starter as $starter){
echo '<h3>'.$starter->name.'</h3><br><p>'.$starter->price.'</p><br>';
} ?>
try below code:
$json = '{
"name": "Takeaway Kings",
"menu": [
{
"starter": [
{
"name": "Samosas",
"price": 3.5
},
{
"name": "Chaat",
"price": 1.99
}
]
},
{
"dessert": [
{
"name": "Kulfi",
"price": 2.5
},
{
"name": "Kheer",
"price": 2.99
}
]
},
{
"main": [
{
"name": "Lamb Biryani",
"price": 4.5
},
{
"name": "Chicken Tikka Masala",
"price": 5.99
}
]
}
]
}';
echo '<pre>';
$json_arr = json_decode($json, true);
//print_r(call_user_func_array('array_merge',$json_arr['menu']));
echo "name: " . $json_arr['name'];
echo '<br />';
$menu_array = call_user_func_array('array_merge',$json_arr['menu']);
foreach ($menu_array as $name => $arr) {
echo '<br />';
echo "Menu name: " . $name;
foreach($arr as $v){
echo '<br />';
echo $v['name'].' : '.$v['price'];
}
}
output:
name: Takeaway Kings
Menu name: starter
Samosas: 3.5
Chaat: 1.99
Menu name: dessert
Kulfi: 2.5
Kheer: 2.99
Menu name: main
Lamb Biryani: 4.5
Chicken Tikka Masala: 5.99
Please Use Below Code For use Inner Values
$data=json_decode($data);
foreach($data->menu as $menu){
foreach($menu as $key => $value){
foreach($value as $key_inner => $value_inner){
echo '<h3>'.$value_inner->name.'</h3><br><p>'.$value_inner->price.'</p><br>';
}
}
}
Related
Here is my PHP code:
<?php
session_start();
include"smsciniz/vendor/autoload.php";
include"smsciniz/baglan.php";
//$insert = $DB->query("INSERT INTO sc_ulke(ulke_isim) VALUES(?)", array($_POST["ulke_isim"]));
$ulke = $DB->query("SELECT * FROM sc_ulke");
foreach ($ulke as $value) {
$url = file_get_contents("https://5sim.net/v1/guest/products/".trim($value["ulke_isim"])."/any");
$json = json_decode($url,true);
echo $json;
}
?>
And here is my JSON data:
"1688": {
"Category": "activation",
"Qty": 2000,
"Price": 14.83
},
"1xbet": {
"Category": "activation",
"Qty": 11413,
"Price": 19
},
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
"888casino": {
"Category": "activation",
"Qty": 4109,
"Price": 5
},
"99app": {
"Category": "activation",
"Qty": 5437,
"Price": 5
},
I want to print the area I marked but I don't know how to do.
I would be very happy if you could help me with an example, thank you in advance
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
We can iterate and find by particular key and print result using json_encode with JSON_PRETTY_PRINT
function findObjByKey($res, $keyToFind){
foreach ($res as $key => $value) {
if($key == $keyToFind) {
return $value;
}
}
return NULL;
}
$json = json_decode('
{"1688": {
"Category": "activation",
"Qty": 2000,
"Price": 14.83
},
"1xbet": {
"Category": "activation",
"Qty": 11413,
"Price": 19
},
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
"888casino": {
"Category": "activation",
"Qty": 4109,
"Price": 5
},
"99app": {
"Category": "activation",
"Qty": 5437,
"Price": 5
}}'
);
$res = findObjByKey($json, '32red');
$prittyPrint= json_encode($res, JSON_PRETTY_PRINT);
echo "<pre>".$prittyPrint."<pre>";
use json_encode($json['32red']) like this you can access any key in json
I'm trying to update a json file and I'm using laravel command to do it. In that file there are specific product codes that need to be changed. The code I'm doing to do this isn't working, it runs but nothing changes.
Here is my code
$old_code = 'P001';
$new_code = 'P011';
$productJson = json_decode(file_get_contents(storage_path('/Product 1/product.json')));
foreach($productJson as $key => $value){
str_replace($old_code, $new_code, $k);
}
file_put_contents(storage_path('/Product 1/product.json), json_encode($productJson, JSON_PRETTY_PRINT));
and this is my json file
{
"P001": {
"name": "Product 1",
"price": "200",
"category": "Shirts"
},
"P002": {
"name": "Product Test",
"price": "100",
"category": "Tops"
},
}
Its probably simpler to read the file and create a new JSON with the new codes like this
$s = '{"P001": {
"name": "Product 1",
"price": "200",
"category": "Shirts"
},
"P002": {
"name": "Product Test",
"price": "100",
"category": "Tops"
},
"P003": {
"name": "Product Test",
"price": "50",
"category": "Bottoms"
}
}';
$old_codes = ['P001', 'P002' ];
$new_codes = ['P011', 'P022' ];
$productJson = json_decode($s);
$new = new stdClass;
foreach($productJson as $key => $json){
$kk = array_search($key, $old_codes);
if ( FALSE !== $kk ) { // found
$new->{$new_codes[$kk]} = $json;
} else {
$new->{$key} = $json;
}
}
echo json_encode($new, JSON_PRETTY_PRINT);
//file_put_contents(storage_path('/Product 1/product.json'), json_encode($new, JSON_PRETTY_PRINT));
RESULT
{
"P011": {
"name": "Product 1",
"price": "200",
"category": "Shirts"
},
"P022": {
"name": "Product Test",
"price": "100",
"category": "Tops"
},
"P003": {
"name": "Product Test",
"price": "50",
"category": "Bottoms"
}
}
What i'm trying to do is get the each array key and then associate the first "price" and "quantity" with each array key.
My array:
{
"32557580": {
"bids": [
{
"price": 2500,
"quantity": 38265661
},
{
"price": 2439,
"quantity": 57414444
},
{
"price": 2381,
"quantity": 1092179
},
{
"price": 2174,
"quantity": 27140648
}
],
"offers": [
{
"price": 2564,
"quantity": 33634591
},
{
"price": 2597,
"quantity": 27842125
},
{
"price": 2632,
"quantity": 925092
},
{
"price": 2667,
"quantity": 3565173
},
{
"price": 2778,
"quantity": 27589980
}
]
},
"32557581": {
"bids": [
{
"price": 4854,
"quantity": 33786947
},
{
"price": 4808,
"quantity": 22881344
},
{
"price": 4762,
"quantity": 2513747
},
{
"price": 4717,
"quantity": 2650000
},
{
"price": 4587,
"quantity": 15714786
}
],
"offers": [
{
"price": 4950,
"quantity": 31749492
},
{
"price": 5000,
"quantity": 3193999
},
{
"price": 5051,
"quantity": 2292463
},
{
"price": 5102,
"quantity": 34770816
},
{
"price": 5128,
"quantity": 2605693
}
]
},
"32557582": {
"bids": [
{
"price": 2532,
"quantity": 60354703
},
{
"price": 2500,
"quantity": 113667648
},
{
"price": 2439,
"quantity": 5125100
},
{
"price": 2222,
"quantity": 120803051
}
],
"offers": [
{
"price": 2564,
"quantity": 1492990
},
{
"price": 2597,
"quantity": 22121811
},
{
"price": 2632,
"quantity": 42119270
},
{
"price": 2667,
"quantity": 43680406
},
{
"price": 2703,
"quantity": 1176966
}
]
}
}
Example:
Id: 32557580
Price: 2500
Quantity: 38265661
Id: 32557581
Price: 4854
Quantity: 33786947
etc..
This is what i got so far
$obj = json_decode($result,true);
$all_keys = array_keys($obj);
foreach ($all_keys as $key => $value) {
echo 'Id: '.$value.'<br>';
}
Output from this is:
Id: 32557580
Id: 32557581
Id: 32557582
I'm not really sure where to go from here, i have tried look at multiple questions and answers elswere.
I tried adding another foreach inside the one i got but i only got the price and quantity from the first array key.
All help with getting me on the right track is appreciated.
Just loop the main array and get the first from bids and use that:
foreach($obj as $key => $value) {
$first = reset($value['bids']);
echo "ID: $key<br />";
echo "Price: " . $first['price'] . "<br />";
echo "Quantity: " . $first['quantity'] . "<br />";
}
Or use the index, which in this case is 0 but may not always be depending on the JSON:
foreach($obj as $key => $value) {
echo "ID: $key<br />";
echo "Price: " . $value['bids'][0]['price'] . "<br />";
echo "Quantity: " . $value['bids'][0]['quantity'] . "<br />";
}
$json_string = '{
"response_code": 200,
"info": {
"days": [
{
"code": "A",
"runs": "111"
},
{
"code": "B",
"runs": "222"
},
{
"code": "C",
"runs": "333"
}
],
"name": "SUPER MARIO",
"number": "010203",
"classes": [
{
"points": "6523",
"name": "ABC",
"available": "N"
},
{
"points": "4253",
"name": "XYZ",
"available": "N"
},
{
"points": "2323",
"name": "JOHN",
"available": "N"
},
{
"points": "5236",
"name": "TAMIL",
"available": "N"
}
]
}
}';
$jsondata = $json_string;
$arr = json_decode($jsondata, true);
foreach($arr as $k=>$v)
{
echo $k."<br>";
}
it prints
response_code
info
But, I need the results like this
6523 ABC
4253 XYZ
2323 JOHN
5326 TAMIL
I have tried and achieved this above results using this below code. But, I want to do it using foreach loop. How do list out all information using foreach?
echo "".$arr['info']['classes'][0]['points']." ".$arr['info']['classes'][0]['name']."<br/>";
echo "".$arr['info']['classes'][1]['points']." ".$arr['info']['classes'][1]['name']."<br/>";
echo "".$arr['info']['classes'][2]['points']." ".$arr['info']['classes'][2]['name']."<br/>";
echo "".$arr['info']['classes'][3]['points']." ".$arr['info']['classes'][3]['name']."<br/>";
You should foreach your array key
foreach($arr['info']['classes'] as $k=>$v)
{
echo $v['points']." " . $v['name']."<br>";
}
I want to parse an array with PHP's foreach loop to get the object names and values inside the 'ques' array.
[
{
"ques": [
{
"name": "comment",
"value": "comment me for the reason",
"sur_id": "1",
"user_id": "admin#gmail.com",
"pagename": "question_response"
},
{
"name": "check-box[]",
"value": "1"
},
{
"name": "radio",
"value": "radio 2"
},
{
"name": "yes",
"value": "no"
}
]
"ques":[
{
"name": "date",
"value": "2015-10-23"
},
{
"name": "select-deopdown",
"value": ""
},
{
"name": "true",
"value": "false"
},
{
"name": "number",
"value": "55"
}
]
}
]
I want to separate the value from the 'ques' array:
while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
// print_r($content);
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content, true);
foreach($datas->ques as $values)
{
echo $values->value . "\n";
print_r($values);
}
$test[] = array('ques' => $datas ,'answer'=>$values);
}