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
Related
I want a list of every player and the time.
I tried
<?php foreach ($status->players as $player) { echo $player.'<br />'; } ?>
But that didn't work for me. Does anybody know how I can get a list of the players and the time?
"name":"Xanth Cluster-Genesis Pt 2 1.5xXP 4xT 3xG - (v342.2)",
"map":"Gen2",
"password":false,
"maxplayers":50,
"players":[
{
"name":"Cody",
"raw":{
"score":0,
"time":5477.548828125
}
},
{
"name":"antoniogelardi9",
"raw":{
"score":0,
"time":4328.5205078125
}
},
{
"name":"yrittz",
"raw":{
"score":0,
"time":671.528564453125
}
}
],
"bots":[
],
"connect":"0000",
"ping":145
}
This way:
<?php
$inputJson =
<<<JSON
{
"name":"Xanth Cluster-Genesis Pt 2 1.5xXP 4xT 3xG - (v342.2)",
"map":"Gen2",
"password":false,
"maxplayers":50,
"players": [
{
"name":"Cody",
"raw":{
"score":0,
"time":5477.548828125
}
},
{
"name":"antoniogelardi9",
"raw":{
"score":0,
"time":4328.5205078125
}
},
{
"name":"yrittz",
"raw":{
"score":0,
"time":671.528564453125
}
}
],
"bots":[
],
"connect":"0000",
"ping":145
}
JSON;
$arr = json_decode($inputJson);
foreach ($arr->players as $player) {
echo $player->name . ": " . $player->raw->time . "\n";
}
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'
I am trying to get a data that is in an array inside another array, I did a search by the forum and google and I made several attempts without success.
It seems like an easy catch but I'm breaking my head all day and I can not.
I'm leaving below my JSON code and my last attempt to pass to PHP.
Thankful.
CODE JSON:
{
"ordens_de_servico": [
{
"oser_numero_os": 23932944,
"oser_nome_solicitante": "ED PI - CAMPO MAIOR",
"oser_dt_abertura": "27/03/2018",
"oser_pontos_refer_endereco": null,
"oser_observ_reclamacao": null,
"oser_prioridade": null,
"servico": {
"serv_cod_servico": 60,
"serv_descr_servico": "CORTE POR DEBITO"
},
"cliente": {
"nome": "ANTONIO WELTON DA SILVA OLIVEIRA",
"telefone": " "
},
"unidade_consumidora": {
"unid_ident_uc": 10945024,
"logr_nome_logr_expandido": null,
"medidor": "A2006171",
"latitude": " -4.711808",
"longitude": "-41.793455"
},
"faturas": [
{
"total_fatura": "23.01"
},
{
"total_fatura": "17.88"
},
{
"total_fatura": "23.01"
},
{
"total_fatura": "21.9"
},
{
"total_fatura": "22.92"
}
]
}
]
}
CODE PHP
<?php
// Read JSON file
$json = file_get_contents('oss.json');
//Decode JSON
$json_data = json_decode($json,true);
//Print data
print_r($json_data);
$os = $json_data['ordens_de_servico'][0]['oser_numero_os']. PHP_EOL;
$data = $json_data['ordens_de_servico'][0]['oser_dt_abertura']. PHP_EOL;
$cod_serv = $json_data['ordens_de_servico']['servico'][0]['serv_cod_servico']. PHP_EOL;
$total_fatura = $json_data['ordens_de_servico']['faturas'][0]['total_fatura']. PHP_EOL;
echo $os."<p>";
echo $data."<p>";
echo $cod_serv."<p>";
echo $total_fatura."<p>";
?>
I tried looping unsuccessfully on the fatura
$json_data = json_decode($json,false);
foreach ( $json_data->ordens_de_servico as $valor){
echo 'FATURA:'.$valor->faturas->total_fatura."<p>".PHP_EOL;
echo PHP_EOL;
}
At a minimum, you are missing the [0] on these two lines:
$cod_serv = $json_data['ordens_de_servico']['servico'][0]['serv_cod_servico']. PHP_EOL;
$total_fatura = $json_data['ordens_de_servico']['faturas'][0]['total_fatura']. PHP_EOL;
They need to be:
$cod_serv = $json_data['ordens_de_servico'][0]['servico']['serv_cod_servico']. PHP_EOL;
$total_fatura = $json_data['ordens_de_servico'][0]['faturas'][0]['total_fatura']. PHP_EOL;
Example of looping through elements in arrays:
foreach($json_data['ordens_de_servico'] as $key => $value){
echo $value["oser_numero_os"];
foreach($json_data['ordens_de_servico'][$key]['faturas'] as $index => $row){
}
}
<?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
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";
}
?>