Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
why I cant get the result I want with this code?
for($i=0; $i<count($results); $i++){
if($results[$i]->high == $results[$i]->open and $results[$i]->low == $results[$i]->close and $results[$i]->vol == 0){
unset($results[$i]);
}
echo json_encode($results);
why I still get the same result as if the unset inside the loop not working? but if I call the echo inside the loop its work
With the sample json data you provided, I've added a DUMMY record that matches the conditions in the if, so it will be unset as follows:
<?php
$json = '[
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:17:00",
"open":2890,
"high":2890,
"low":2880,
"close":2890,
"vol":260100
},
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:18:00",
"open":2880,
"high":2890,
"low":2880,
"close":2880,
"vol":288300
},
{
"code":"DUMMIE",
"_date":"2020-11-09",
"_time":"09:19:00",
"open":2890,
"high":2890,
"low":2880,
"close":2880,
"vol":0
},
{
"code":"TLKM",
"_date":"2020-11-09",
"_time":"09:19:00",
"open":2890,
"high":2890,
"low":2880,
"close":2890,
"vol":3070200
}
]';
// turn json into associative array
$arr = json_decode($json, true);
// process the array
foreach ($arr as $key => $value) {
if ($arr[$key]['high'] == $arr[$key]['open'] and $arr[$key]['low'] == $arr[$key]['close'] and $arr[$key]['vol'] == 0) {
unset($arr[$key]);
}
}
// array back to json format
$json = json_encode($arr);
Have a look at the demo
Output:
{
"0": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:17:00",
"open": 2890,
"high": 2890,
"low": 2880,
"close": 2890,
"vol": 260100
},
"1": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:18:00",
"open": 2880,
"high": 2890,
"low": 2880,
"close": 2880,
"vol": 288300
},
"3": {
"code": "TLKM",
"_date": "2020-11-09",
"_time": "09:19:00",
"open": 2890,
"high": 2890,
"low": 2880,
"close": 2890,
"vol": 3070200
}
}
Related
This question already has an answer here:
Loop through nested arrays PHP
(1 answer)
Closed 12 months ago.
I have the json array below and im trying to access the ProductResults bit.
I want the output to be somthing like this:
Term: 47, Type:HP, Payment: 229.4
Term: 47, Type:PCP, Payment: 172.23
Term: 60, Type:PCP, Payment: 186.82
But im struggeling to even access other parts of the array.
PHP:
$json = json_decode($resp, true);
foreach ($json['VehicleResults'] as $item)
{
$data = $item['FinanceProductResults'];
$v0 = $data['Term'];
echo $v0;
}
JSON:
{
"VehicleResults": [{
"Id": "0",
"FinanceProductResults": [{
"Term": 47,
"AnnualMileage": 6000,
"Deposits": 1000,
"ProductResults": [{
"Key": "HP",
"Payment": 229.4
}, {
"Key": "PCP",
"Payment": 172.23
}]
}, {
"Term": 60,
"AnnualMileage": 6000,
"Deposits": 1000,
"ProductResults": [{
"Key": "HP",
"Payment": 186.82
}]
}]
}]
}
The FinanceProductResults is also an array so it needs to be accessed also as an array. Like you have accessed VehicleResults and same goes for ProductResults. So your code should look something like this.
$json = json_decode($resp, true);
foreach ($json['VehicleResults'] as $item)
{
$dataItems = $item['FinanceProductResults'];
foreach ($dataItems as $data) {
$v0 = $data['Term'];
foreach ($data['ProductResults'] as $productResult) {
$type = $productResult['Key'];
$payment = $productResult['Payment'];
echo "Term: $v0, Type: $type, Payment: $payment";
}
}
}
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I have this kind of json objects and im trying to search an item under sales->products->id but i can't make it work. I hope someone could help me. Thanks!
"sales": [
{
"ID": 123456,
"transaction_id": "123456789",
"key": "sdawa57sd547sad4sadx54ad",
"subtotal": 20,
"tax": "0",
"fees": null,
"total": "20",
"gateway": "paypal",
"email": "email#email.com",
"date": "2018-08-01 13:13:55",
"discounts": null,
"products": [
{
"id": 1234,
"quantity": 1,
"name": "Product 1",
"price": 20,
"price_name": ""
}
]
}
This is the code i used:
$content = file_get_contents($url);
$results= json_decode($content, TRUE);
foreach($results->sales as $item)
{
if($item->products->id == "1234")
{
echo $item->products->name;
}
}
Because you've passed the second parameter as true to json_decode, your $results variable is an array, so you need to access it like that:
foreach($results['sales'] as $item) {
foreach ($item['products'] as $product) {
if ($product['id'] == '1234') echo $product['name'];
}
}
Output:
Product 1
Demo on 3v4l.org
If you want to find which sales have the product, then you should do this
$results= json_decode($content);
$productid = "1234"; //product you want to search for
foreach($result->sales as $sale)
{
$keys = array_keys(array_column($sale->products, 'id'), $product_id);
if(!empty($keys))
$saleIDs[] = $sale->ID;
}
var_dump($saleIDs); //contains all the sale IDs that have the product
Read about array_keys and array_column.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i have a JSON with this data:
$data = "meta_data":[
{
"id":2279,
"key":"codice_fiscale",
"value":"gege"
},
{
"id":2280,
"key":"numero_tessera_titolare",
"value":"gre"
},
{
"id":2281,
"key":"classe_tessera_titolare",
"value":"gre"
},
{
"id":2282,
"key":"tratta_da",
"value":"gre"
},
{
"id":2283,
"key":"tratta_a",
"value":"grge"
},
{
"id":2284,
"key":"studente",
"value":"studente"
}];
I need to loop all "key" and when i find the key that i need (in this case "studente") i need to get the value stored in "value".
How can i do this in PHP?
EDIT
I tried this:
foreach($data as $row){
if($row->key=='studente'){
$var = $row->value;
}
}
The best / most efficient way is to write a foreach loop with a break.
Code: (Demo)
$json = '{
"meta_data": [
{
"id": 2279,
"key": "codice_fiscale",
"value": "gege"
},
{
"id": 2283,
"key": "tratta_a",
"value": "grge"
},
{
"id": 2284,
"key": "studente",
"value": "studente"
}
]}';
$data = json_decode($json, true);
$search='studente';
$found=false;
foreach($data['meta_data'] as $d){
if($d['key']==$search){
$found=$d['value'];
break;
}
}
echo $found?$found:"$search not found";
Output:
studente
First, $data is not a valid json. You can use json_decode() to get a php array representation of your json string. Then loop through array.
Example:
$json = '{
"meta_data": [
{
"id": 2279,
"key": "codice_fiscale",
"value": "gege"
},
// other items...
{
"id": 2283,
"key": "tratta_a",
"value": "grge"
},
{
"id": 2284,
"key": "studente",
"value": "studente"
}
]}';
$data = json_decode($json, true);
//var_dump($data);
foreach($data['meta_data'] as $key => $item) {
if (isset($item['value']) && $item['value'] == 'studente') {
var_dump($item);
}
}
This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 6 years ago.
I want print specific vars from array inside a document.
JSON structure:
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
I am trying to do this in PHP (and already decoded that json). How can I access and print all ids using foreach or for?
I can not understand how I can manage "return" scope.
You can decode the json contents and use it like an array. This should work:
$json = json_decode($string, true);
$contacts = $json['return']['contacts'];
foreach($contacts as $contact){
echo $contact['contact']['id'];
}
Json decode to array:
$json = json_decode($strjson, true); // decode the JSON into an associative array
and
echo "<pre>";
print_r($json);
And foreach:
foreach ($json as $key => $value) {
echo $key . " ". $value. "<br>";
}
Working example:
$json = '{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
}
]
}
}';
$json = json_decode($json, true);
print_r($json);
foreach ($json['return']['contacts'] as $val) {
echo $val['contact']['id']."<br>";
}
Try this code:
$json ='
{
"return": {
"string": "2222",
"contacts": [
{
"contact": {
"id": "09890423890"
}
},
{
"contact": {
"id": "2423444"
}
},
{
"contact": {
"id": "24242423"
}
},
etc
]
}
}
';
$array = json_decode($json,TRUE);
foreach($array['return']['contacts'] as $value ){
echo $value['id'];
}
Use file_get_contents function if you want to pull data from file.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
Hi tried to request each src value from wordpress media playlist , so i return data in JSON
My request Code which did not succeed was :
$decode_tracks = json_decode($track , true);
foreach ($decode_tracks[0][0] as $item) {
print $item['src'];
}
This is some of the data that was returned.
[
{
"src": "http:\/\/localhost\/wordpress\/wp-content\/uploads\/2015\/08\/output.mkv",
"type": "video\/x-matroska",
"title": "output",
"caption": "",
"description": "",
"meta": {
"length_formatted": "0:16"
}
},
{
"src": "http:\/\/localhost\/wordpress\/wp-content\/uploads\/2015\/08\/christmas.mp4",
"type": "video\/mp4",
"title": "christmas",
"caption": "tytj",
"description": "tyjtyj",
"meta": {
"length_formatted": "1:21"
}
}
]
Try this:
foreach ($decode_tracks as $item) {
print $item['src'];
}
There is no need to convert json to array, you can easily do like this:
foreach ($track as $item) {
print $item->src;
}
$decode_tracks = json_decode($track);
for ($i = 0; $i < count($decode_tracks); $i++) {
print $decode_tracks[$i]->src;
}
Or if you are using array:
try this one
$decode_tracks = json_decode($track, true);
for ($i = 0; $i < count($decode_tracks); $i++) {
print $decode_tracks[$i]['src'];
}