How to get json data in blade laravel - php

In the blade template:
<?php
$meta = json_encode($datas,true);
echo $meta;
?>
It shows:
{"data":[{"id":1,"title":"Alice.","rating":0},
{"id":2,"title":"So Alice.","rating":2},
{"id":3,"title":"Alice.","rating":2},
{"id":4,"title":"After a.","rating":2},
{"id":5,"title":"Alice.","rating":0}],
"meta":{"song_count":5}}
My question is how to get (song_count: 5 ) in blade template.
I have tried :
echo $meta['meta'];
"
it shows;
"Illegal string offset 'meta'"
Anyone can help, thank you so much

json_encode() method give you a json formatted data. You meed to decode it :
$meta = json_encode($datas,true);
$meta = json_decode($meta, true);
echo $meta;
Now you acess the array property as $meta['meta'];
NB : if $datas is already an array, then you don't need to encode and decode

thats mean you try to take meta key from string because $meta is json not array so you dont need to json_encode $data , you can do direct :-
$datas['meta']['song_count'] if $data is array . but this maybe give you error if $datas is empty to solve this you can make check if $data !empty or use optional method in laravel like this :- optional($datas['meta'])['song_count'] it solve the problem .

The steps to show the value of json data from controller as bwlow:
<?php
$datas = json_encode($datas,true);
//dd($data);
$datas = json_decode($datas);
// dd($datas);
foreach($datas->meta as $link){
echo $link;
}
?>

Related

PHP Array inside of an array issue

I'm having some problems getting the data through my PHP loop.
<?php
$url = 'https://www.fibalivestats.com/data/1653309/data.json';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<?php
foreach($json['totallds']['sPoints'] as $item); {
echo $item;
}
?>
The error I'm getting is an array to string conversion error. What I'm trying to get is the data from the sPoints array that will give me a Top 5 points scorers for a basketball game.
I'll build a table for this in HTML later but for now, it's not displaying the data at all and I'm getting errors. I feel like I may have confused arrays and strings too. Any thoughts about what I'm doing wrong? JSON file can be found in the $url variable.
Also if it helps, here's the link to where I have gotten the data from and what context the Top 5 is from https://www.fibalivestats.com/u/NSS/1653309/lds.html
Thanks!
Your $item is an array, so you can't just echo it like that. You can, however, echo its columns, for example:
foreach($json['totallds']['sPoints'] as $item) {
echo $item['firstName'] . ' ' . $item['familyName'];
}
Notice the removed semicolon between the foreach () and {.
Well, array to string conversion error means you're trying to echo an array.
If you see the return of the url you are looking at, you can verify that the "sPoints" key returns an array with several objects.
Try to change echo to print_r or var_dump to view entire data or complete your business logic.
Try changing:
echo $item;
to:
var_dump($item);

How to display JSON array

So i have written this code to get a json from API.AI. i want to know how to echo out the decoded JSON file on this form. Currently the decoded JSON is being saved in a new file JSON.php, but instead of saving i want to know how to view it?
$data = json_decode(file_get_contents("php://input"), true);
file_put_contents("JSON.php", print_r($data, true));
As comments state you can use var_dump to view the rough data but if you actually wanna use it for something you might wanna loop over the object and extracte properties.
like so:
foreach($data as $theData)
{
echo $theData->property;
}
Regards
do like this:-
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
print_r($request);
or you can do :- $request->indexname

How to get value from JSON encode in PHP?

i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.

PHP json_encoded object not passing to other page

I'm using json_encode on an object and stored on a hidden text field, When i was passing to the next page i didn't get any data
Code:
$flight = json_encode($od->FlightSegments);
Response page:
<?php print_r($_POST); ?> printed Array ( [flight] => { )
serialize and unserialize not working on my object.
Can any one tell me what's going wrong ?
To see XML literally on the web page, use:
<?php echo '<pre>' . htmlentities(print_r($_POST, true)) . '</pre>'; ?>
Since serialize and unserialize are not giving me the correct output for me, I solved my problem using below code.
$count = 0;
$_SESSION['fl'] = [];
foreach($response->Response__Depart->OriginDestinationOptions->OriginDestinationOption as $od){
$flight = json_encode($od->FlightSegments);
$_SESSION['fl'][$count] = $flight;
}
Stored entire object into a session array

Getting Value From Json Data Inside Array

I have a problem to get the value from json data inside array when the data is more than one.
When my data only one like this:
$mydata='[{"firstName":"Ana","height":5.3}]';
I can just access the height of Ana by substring-ing it first and the decode it, like this:
$mydata= substr($mydata, 1, -1);
$obj = json_decode($mydata);
print $obj->{'height'};
The problem is when the data look like this:
$mydata='[{"firstName":"Ana","height":5.3},{"firstName":"Taylor","height":5.11}]';
How can I get the height of Ana?
print $obj->{0}->{'height'}; //doesn't work.
Please help. Thankyou in advance.
use json_decode
$b_arr=json_decode($mydata,true);
$b_arr[0]['height'];//0 is index for array
You can convert with json_decode and iterate through your array to get your specific data:
<?php
$mydata='[{"firstName":"Ana","height":5.3},{"firstName":"George","height":7.3}]';
$json = json_decode($mydata, true);
foreach($json as $key => $value) {
if($value['firstName'] == 'Ana') {
echo $value['height'];
break;
}
}
?>

Categories