i have a pretty weird json here. Im trying to iterate through every subscription object and get some data
Here is the json
{
"result":"success",
"data":{
"subscriptions":[
{
"name":"app_generator",
"expirey":1628188363942,
"hwids":3,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"efede5757f512f9eae1d600302ca5c4e6a3cadb0658310c91c6da2ada68175f4"
]
},
{
"name":"apple",
"expirey":1625596518277,
"hwids":2,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515a"
]
}
]
}
}
The code i tried was:
foreach($json['result']['data']['subscriptions'] as $a)
{
//print_r($a);
$expirey = $a['expirey']
}
Im just getting a lot of errors. Let me know what the correct approach would be. My goal is to mainly get the name & expirey data
Related
I am working in the PHP language.
The object is
"[{"value":"test_1"},{"value":"test_2"}]"
and I want
['test_1', 'test_2']
Here is my code...
foreach(json_decode($email) as $emails){
$arr[]=$emails->value;
}
$final = json_encode($arr);
The Output is
"["test_1","test_2"]"
I am not satisfied with this output.
If anyone can help me please give an answer.
assuming that You have the data of array of objects like
$array=[
{
"value":"value1"
},
{
"value":"value2"
}
]
simple use the function pluck() to retrieve data as you want .
$value_array=$array->pluck("value");
Your All keys need to be same to retrieve all values
the $value=["value1","value2"]
i'm trying to get the fields "name, price, image and rarity" to show in a php file, anyone can help me? Thanks ;D
{
"status": 300,
"data": {
"date": "2019-09-16T00:00:00.000Z",
"featured": [
{
"name": "Flying Saucer",
"price": "1,200",
"images": {
"icon": icon.png",
},
"rarity": "epic",
},
I'm using this that a friend told me, but i cant put that to work :c
<?php
$response = json_decode(file_get_contents('lista.json'), true);
foreach ($response as $val) {
$item = $val['name'];
echo "<b>$item</b>";
}
?>
I'm not quite sure what you are trying to achieve. You can just access the contents via the $response array like this:
echo $response['status']; // would output 300
You can use foreach to iterate through the array. For example: If you want to output the name of every element of the array you can use:
foreach ($response['data'] as $val) { // loop through every element of the data-array (if this makes sense depends on the structure of the json file, cant tell because it's not complete)
echo $val['featured']['name'];
}
You gotta get the index in $val['data']['featured']['name'] to retrieve the name index.
When you defined the second parameter of json_decode, you said that you want your json to be parsed to an array. The brackets in the original json identify when a new index of your parsed array will begin.
I suggest you to read about json_decode and json in general:
JSON: https://www.json.org/
json_decode function: https://www.php.net/manual/en/function.json-decode.php
I am wondering how to filter through a result like this properly. What I am doing results in problems. Obviously using a foreach on an array with a single item is not ideal but I don't know exactly how to do it any other way.
// connect with the static file
$json = file_get_contents('resources/assets/datafeeds/brewery_single.json');
$obj = json_decode($json, true);
// create brewery info
foreach($obj['pages'] as $brewery) {
foreach($brewery['results'] as $brewery) {
}
}
I've tried doing something like:
foreach($obj['pages']['results'] as $brewery) {
}
But I get an Undefined index: result errror like this.
Example data with only one result block, my data has multiple:
{
"apiName": "brewery_single",
"apiGuid": "d74e8aa2-4064-44b9-81de-2ceb150882c0",
"generatedAt": 1452761620,
"pages": [
{
"pageUrl": "http://www.brewery-website.com",
"results": [
{
"website": "www.brewery.com/",
"plaats": "Place",
"latlong": [
"53.657856",
"5.032597"
],
"naam": "Brouwerij title",
"provincie": "Noord Brabant",
"afbeelding": "http://www.brewery.com/images/brewery/brewer.jpg",
"actief": "Yes",
"land": "Netherlands",
"adres": "<p><b>Adres:</b><br />Street 40 <br />2388 GP<br /> Province, Netherlands </p>",
"opgericht": "1990"
}
]
},
]
}
Hopefully somebody can help me out so I can properly get my data from an api.
It seems like pages is an array of potentially multiple pages, each of which contains a results array of potentially multiple results. So indeed, two loops are in order:
foreach ($obj['pages'] as $page) {
foreach ($page['results'] as $brewery) {
...
}
}
If you're only ever interested in the first result, you can try to directly access it:
if (isset($obj['pages'][0]['results'][0])) {
echo $obj['pages'][0]['results'][0]['plaats'];
}
But again, the point of this data structure appears to be to account for multiple results, so you may be missing out on some information if you only ever consider the first.
Consult the documentation (which hopefully exists) of that API for exact details on the returned data.
Add an extra check to see if the data that you want is in the array:
if (isset($obj['pages'])) {
foreach($obj['pages'] as $brewery) {
if (isset($brewery['results'])) {
foreach($brewery['results'] as $brewery) {
}
}
}
}
There is syntax error in your code near ['results]
Change from
foreach($obj['pages']['results] as $brewery) {
into
foreach($obj['pages']['results'] as $brewery) {
There is no multiple arrays in results array. So why you need foreach there?
Anyways you can get value of results by providing exact INDEX.
For example: echo $obj['pages']['results]['website'];
I need to parse a JSON file. I've only worked with XML before.
How can I get the second "food_id" (1730905)?
Here is my JSON file:
{
"shopId":29,
"last":46977914,
"freshfood":[
{
"freshfood_id":2629,
"food":[
{
"food_id":1740851,
"type":"fruit",
"status":1
},
{
"food_id":1730905,
"type":"vegetable",
"status":1
},
]
}
]
}
I tried this, but it does not work.
$string = file_get_contents("food.json");
$json_a=json_decode($string,true);
echo $GetFreshFoodId = $json_a['freshfood'][1]['freshfood_id'];
PHP arrays are zero-based, so that should be:
$json_a['freshfood'][0]['food'][1]['food_id'];
Also, note that the JSON is not entirely valid - you should remove the last comma. (But you might have left out additional records in your example JSON for clarity.)
{
"Group": [
{
"name": "HolderOne",
"operators": [
{
"username": "ken",
"status": 3
},
.....etc.....
Hi all,
I've succesfully pulled data out of my JSON feed thanks the help on SO...but...scratching my head now.
By using print_r($obj->....) I am able to extract all information from my feed BUT what I want to be able to do is...
From above, if the value of username is 'ken' then only display the associated status 3.
I think it could be using the value the print_r assigns each Array(?) e.g. above would be [0] - now I don't know what the value would be so can I grab the [n] value for each username to display the status?
I am slightly outside my comfort zone here...not sure if it's a PHP or JSON problem for a start.
Thanks in advance for any help
Is this what you are looking for?
foreach($obj->Group as $group)
{
foreach($group->operators as $operator)
{
if($operator->username == "ken")
{
echo $operator->status;
}
}
}