<?php
$jsondata = file_get_contents('js/brand.json');
$data = json_decode($jsondata, true);
$menuonesublinks = $data['menuonesublinks']['links'];
foreach ($menuonesublinks as $menu_sublink) {
?>
<?php
echo $menu_sublink['title'];
?>
<?php
}
?>
o/p i am getting : usa,uk
how can i get inside subcities of city values ("subcities":[{"city":"dubai"},{'city':"london"}])
brand.json
{
"menuonesublinks": {
"links": [
{
"title": "usa",
"subcities":[{"city":"dubai"},{'city':"london"}]
},
{
"title": uk
}
]
}
}
You need to change your foreach loop in the following way,
// your code
foreach ($menuonesublinks as $menu_sublink) {
echo $menu_sublink['title'];
if(array_key_exists('subcities', $menu_sublink)){
foreach($menu_sublink['subcities'] as $subcity){
// Now access subcities like this: $subcity['city']
}
}
}
Here's the relevant reference:
http://php.net/manual/en/function.array-key-exists.php
Related
I got some data in my data/clients.json file and I'm trying to reach for some of the keys and values and display them in my php file. It does not work and i can not figure out why.
My clients.json file looks like:
{
"data": {
"nikajakubec": {
"name": "nika",
"loans": {
"amount": "1000",
"reason": "for garden"
}
}
}
}
And my PHP file:
<?php
$sData = file_get_contents('data/clients.json');
$jData = json_decode($sData);
$jClient = $jData->data->$sUsername;
foreach ($jClient->loans as $jLoans) {
echo "
<div>
<div>Amount:$jLoans->loans-></div>
<div>Reason:$sUserName->reason</div>
</div>";
}
?>
Do like this:-
<?php
$json = '{
"data": {
"nikajakubec": {
"name": "nika",
"loans": {
"amount": "1000",
"reason": "for garden"
}
}
}
}';
$jData = json_decode($json);
foreach ($jData->data as $user) {
echo "
<div>
<div>Amount:". $user->loans->amount ."</div>
<div>Reason: ". $user->loans->reason ."</div>
</div>";
}
Output:- https://3v4l.org/8HPV5
The field reason it's actually nested inside the loans prop, not directly in the user.
so instead of
$sUserName->reason
just go
$jLoans->reason
And also go $jLoans->amount instead of $jLoans->loans->
Consider that when you cycle $jClient->loans you are going to work on each prop of loans, which are 'amount' and 'reason'
How to display the names of accounts on user's profile page? So there would be displayed holidays and savings next to Accounts:
I'm new to coding and I'm trying to display the names of accounts (not the id) that user has and it should be displayed on their profile page. The accounts are saved in clients.json file. Thanks for help!
data/clients.json
{
"data": {
"tomas": {
"name": "eter",
"accounts": {
"5c7072a835c3b": {
"balance": 1000,
"name": "savings"
},
"5c7072a835c36": {
"balance": 1000,
"name": "holidays"
}
}
}
}
}
my PHP file :
<?php
session_start();
if (!isset($_SESSION['sUserId'])) {
header('Location: login.php');
}
$sUserId = $_SESSION['sUserId'];
$sData = file_get_contents('data/clients.json');
$jData = json_decode($sData);
if ($jData == null) {
echo 'System update';
}
$sTotalBalance = array_sum(array_column((array)$jData->data->$sUserId->accounts, 'balance'));
require_once 'top.php';
?>
<div><h5>Username:</h5> <?php echo $sUserId; ?></div>
<div><h5>Name: </h5><?php echo $jData->data->$sUserId->name; ?></div>
<div><h5>Accounts: </h5><?php echo $jData->data->$sUserId->accounts; ?> kr </div>
<?php
$sLinkToScript = '<script src="js/profile.js"></script>';
require_once 'bottom.php';
?>
Try iterating it as follows -
foreach($jData->data->$sUserId->accounts as $acc) {
echo $acc->name;
}
Hello I'm trying to parse some key of JSON object, the code works now but I don't want to parse all the keys, just like the first 6.
This is the JSON
{
"results":[
{
"name":"Luke Skywalker",
"height":"172",
"mass":"77",
"hair_color":"blond",
"skin_color":"fair",
"eye_color":"blue",
"birth_year":"19BBY",
"gender":"male"
},
{
"name":"C-3PO",
"height":"167",
"mass":"75",
"hair_color":"n/a",
"skin_color":"gold",
"eye_color":"yellow",
"birth_year":"112BBY",
"gender":"n/a"
},
{
"name":"R2-D2",
"height":"96",
"mass":"32",
"hair_color":"n/a",
"skin_color":"white, blue",
"eye_color":"red",
"birth_year":"33BBY",
"gender":"n/a"
},
{
"name":"Darth Vader",
"height":"202",
"mass":"136",
"hair_color":"none",
"skin_color":"white",
"eye_color":"yellow",
"birth_year":"41.9BBY",
"gender":"male"
},
{
"name":"Leia Organa",
"height":"150",
"mass":"49",
"hair_color":"brown",
"skin_color":"light",
"eye_color":"brown",
"birth_year":"19BBY",
"gender":"female"
},
{
"name":"Owen Lars",
"height":"178",
"mass":"120",
"hair_color":"brown, grey",
"skin_color":"light",
"eye_color":"blue",
"birth_year":"52BBY",
"gender":"male"
},
{
"name":"Beru Whitesun lars",
"height":"165",
"mass":"75",
"hair_color":"brown",
"skin_color":"light",
"eye_color":"blue",
"birth_year":"47BBY",
"gender":"female"
},
{
"name":"R5-D4",
"height":"97",
"mass":"32",
"hair_color":"n/a",
"skin_color":"white, red",
"eye_color":"red",
"birth_year":"unknown",
"gender":"n/a"
},
{
"name":"Biggs Darklighter",
"height":"183",
"mass":"84",
"hair_color":"black",
"skin_color":"light",
"eye_color":"brown",
"birth_year":"24BBY",
"gender":"male"
},
{
"name":"Obi-Wan Kenobi",
"height":"182",
"mass":"77",
"hair_color":"auburn, white",
"skin_color":"fair",
"eye_color":"blue-gray",
"birth_year":"57BBY",
"gender":"male"
}
]
}
and this is my code (when you click on a person of the list you get the details):
<hr>
<h3>Details</h3>
<?php
$id=$_POST['id'];
$url="https://swapi.co/api/people/".$id."/";
$json = file_get_contents($url);
$array = json_decode($json, true);
foreach($array as $a => $b){
?>
<span><?php echo ucfirst($a);?> : <?php echo $b; ?></span>
<br>
<?php
}
?>
I was trying to change the foreach with a line like:
foreach($array['results'] as $a => $b) {
$name=$b['name'];
$height=$b['height];
But get a:
Warning: Invalid argument supplied for foreach() in
This is the JSON data I get from our ticket-system. I would like to parse it with PHP and save the data in a database to create statistics and a dashboard so everyone can see how many tickets are open and the latest closed tickets.
I can read some of the data but not everything.
{
"address":"belgium",
"workers":{
"peter":{
"worker":"peter",
"open_close_time":"45.6 T/h",
"closed_tickets":841,
"open_tickets":7,
"last_checkin":1498768133,
"days_too_late":0
},
"mark":{
"worker":"mark",
"open_close_time":"45.9 T/h",
"closed_tickets":764,
"open_tickets":2,
"last_checkin":1498768189,
"days_too_late":0
},
"walter":{
"worker":"walter",
"open_close_time":"20.0 T/h",
"closed_tickets":595,
"open_tickets":4,
"last_checkin":1498767862,
"days_too_late":0
}
},
"total_tickets":2213,
"tickets":[
{
"id":2906444760,
"client":"297",
"processed":0
},
{
"id":2260,
"client":"121",
"processed":0
},
{
"id":2424,
"client":"45",
"processed":0
}
],
"last_closed_tickets":[
{
"id":2259,
"client":"341",
"closed_on":"2017-06-25T10:11:00.000Z"
},
{
"id":2258,
"client":"48",
"closed_on":"2017-06-20T18:37:03.000Z"
}
],
"settings":{
"address":"belgium",
"email":"",
"daily_stats":0
},
"open_close_time":"161.1 T/h",
"avgopen_close_time":123298,
"ticket_time":"27.1 T/h",
"stats":{
"time":1498768200087,
"newest_ticket":1498768189000,
"closed_tickets":2200,
"open_tickets":13,
"active_workers":3
},
"avg_paid_tickets":64.55,
"avg_afterservice_tickets":35.45
}
This is the PHP code I tried to get the names of the worker but this doesn't work.
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
echo $json['address'];
foreach($json->workers->new as $entry) {
echo $entry->worker;
}
?>
If I try it like here below it works but then I've got to change the code everytime another employee starts.
echo $json['workers']['mark']['closed_tickets'];
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
foreach($json['workers'] as $entry){
echo $entry['worker'] . " has " . $entry['open_tickets'] . " tickets" . "\n";
}
?>
I have a json result from facebook with occasionally a subattachment and I can't seem to access the subattachment to show all the photo's.
Here is a Json example (adjusted it, so it wasn't so long:
{
"data":[
{
"id":"1"
"type":"video",
"attachments":{
"data":[
{
"media":{
"image":{
"src":"https:\/\/scontent.xx.fbcdn.net\/v\/t15.0-10\/s720x720\/"
}
}
}
]
}
},
{
"id":"2"
"type":"photo",
"attachments":{
"data":[
{
"media":{
"image":{
"src":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/q84\/p180x540\/"
}
}
}
]
}
},
{
"id":"3"
"type":"photo",
"attachments":{
"data":[
{
"subattachments":{
"data":[
{
"media":{
"image":{
"src":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/s720x720\/"
}
}
"type":"photo"
},
{
"media":{
"image":{
"src":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/s720x720\/"
}
},
"type":"video"
}
]
}
"type":"album",
}
]
}
}
]}
This is what i so far:
$json_link = "https://graph.facebook.com/{$page_id}/feed?access_token={$access_token}&fields={$fields}";
$json = file_get_contents($json_link);
$obj = json_decode($json);
When I try:
if(isset($obj->data)) {
foreach ($obj->data as $key => $value)
{
$attachments = $value->attachments->data[0];
$att = $attachments->media->image->src;
$images = explode('&url=',$att);
if(isset($images))
{
foreach ($images as $image) {
echo '<img src="' . $image . '" alt="error" /><br>';
}
}
for($i=0;$i<count($value->attachments->data[0]);$i++)
{
$subattachments = $attachments->subattachments->data[$i];
$subatt = $subattachments->media->image->src;
$subimages = explode('&url=',$subatt);
foreach ($subimages as $subimage) {
echo '<img src="' . $subimage . '" alt="error" /><br>';
}
}
}
}
This shows all the photo,s in the attachments, but throws several errors, because the code wants to show the subattachments in the attachments also. How do I filter this, so I don't get any errors?
It also shows just 1 photo in the subattachments. How can I show all the photo's in the subattachments? I tried it with if and if else statements and for loops, for each loops. I can't get it to work the way I want it to.