1

This it´s my json data - file :

>     $fend='{
>       "apollo": {
>           "data": {
>               "ProductItem:B009ZN8U96": {
>                   "id": "B009ZN8U96",
>                   "title": "Lenovo ThinkCentre Tiny Storage Unit Negro - Disco Duro en Red (Negro)",
>                   "brand": "Lenovo",
>                   "tags": {
>                       "type": "json",
>                       "json": ["Discos duros externos", "Almacenamiento de datos externo", "Almacenamiento de datos", "Informática"]
>                   },
>                   "related_items": {
>                       "type": "json",
>                       "json": ["B009ZN8U14", "B00Z08TZNU"]
>                   },
>                   "prices": [{
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96.prices.0",
>                       "generated": true
>                   }, {
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96.prices.1",
>                       "generated": true
>                   }, {
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96.prices.2",
>                       "generated": true
>                   }, {
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96.prices.3",
>                       "generated": true
>                   }, {
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96.prices.4",
>                       "generated": true
>                   }],
>                   "images": {
>                       "type": "id",
>                       "id": "$ProductItem:B009ZN8U96.images",
>                       "generated": true
>                   },
>                   "chart": "https://charts.hagglezon.com/B009ZN8U96.png",
>                   "__typename": "ProductItem"
>               },
>               "ProductItem:B009ZN8U96.prices.0": {
>                   "country": "en",
>                   "price": "22.02",
>                   "currency": "EUR",
>                   "url": "https://www.amazon.co.uk/Lenovo-ThinkCentre-Tiny-Storage-Unit/dp/B009ZN8U96?SubscriptionId=AKIAJKVC5TM5C4Y6TOSQ&tag=haggle-web-en-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B009ZN8U96",
>                   "__typename": "Prices"
>               },
>               "ProductItem:B009ZN8U96.prices.1": {
>                   "country": "es",
>                   "price": "22.71",
>                   "currency": "EUR",
>                   "url": "https://www.amazon.es/Lenovo-ThinkCentre-Tiny-Storage-Negro/dp/B009ZN8U96?SubscriptionId=AKIAJKVC5TM5C4Y6TOSQ&tag=haggle-web-es-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B009ZN8U96",
>                   "__typename": "Prices"
>               },
>               "ProductItem:B009ZN8U96.prices.2": {
>                   "country": "it",
>                   "price": "23.18",
>                   "currency": "EUR",
>                   "url": "https://www.amazon.it/Lenovo-ThinkCentre-Tiny-Storage-Unit/dp/B009ZN8U96?SubscriptionId=AKIAIJYBMV6KUA55XTDA&tag=haggle-web-it-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B009ZN8U96",
>                   "__typename": "Prices"
>               },
>               "ProductItem:B009ZN8U96.prices.3": {
>                   "country": "fr",
>                   "price": "27.25",
>                   "currency": "EUR",
>                   "url": "https://www.amazon.fr/Lenovo-PCW-0B47375-0B47375-Noir/dp/B009ZN8U96?SubscriptionId=AKIAJGIY7FE2CHC4YNTA&tag=haggle-web-fr-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B009ZN8U96",
>                   "__typename": "Prices"
>               },
>               "ProductItem:B009ZN8U96.prices.4": {
>                   "country": "de",
>                   "price": "33.32",
>                   "currency": "EUR",
>                   "url": "https://www.amazon.de/Lenovo-ThinkCentre-Tiny-Storage-Unit/dp/B009ZN8U96?SubscriptionId=AKIAJGIY7FE2CHC4YNTA&tag=haggle-web-de-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B009ZN8U96",
>                   "__typename": "Prices"
>               },
>               "$ProductItem:B009ZN8U96.images": {
>                   "small": "https://images-eu.ssl-images-amazon.com/images/I/31tLd6DMO-L._SL75_.jpg",
>                   "medium": "https://images-eu.ssl-images-amazon.com/images/I/31tLd6DMO-L._SL160_.jpg",
>                   "large": "https://images-eu.ssl-images-amazon.com/images/I/31tLd6DMO-L.jpg",
>                   "__typename": "Images"
>               },
>               "$ROOT_QUERY.searchResults({\"search\":\"B009ZN8U96\",\"lang\":\"es\"})":
> {
>                   "products": [{
>                       "type": "id",
>                       "id": "ProductItem:B009ZN8U96",
>                       "generated": false
>                   }],
>                   "hasMore": false,
>                   "nextPage": null,
>                   "status": "200",
>                   "error": null,
>                   "__typename": "SearchResults"
>               },
>               "ROOT_QUERY": {
>                   "searchResults({\"search\":\"B009ZN8U96\",\"lang\":\"es\"})": {
>                       "type": "id",
>                       "id": "$ROOT_QUERY.searchResults({\"search\":\"B009ZN8U96\",\"lang\":\"es\"})",
>                       "generated": true
>                   }
>               }
>           },
>           "optimistic": [],
>           "reducerError": null
>       },
>       "favs": [],
>       "i18n": {
>           "lang": "en-gb",
>           "locale": "en"
>       },
>       "searchLang": "",
>       "seenFeatures": [],
>       "chart": {},
>       "searchContext": {
>           "title": "Lenovo ThinkCentre Tiny Storage Unit Negro - Disco Duro en Red (Negro)",
>           "tags": ["Discos duros externos", "Almacenamiento de datos externo", "Almacenamiento de datos", "Informática"],
>           "related": ["B009ZN8U14", "B00Z08TZNU"],
>           "nextPage": null,
>           "hasMore": false,
>           "query": "B009ZN8U96",
>           "isSingle": true,
>           "brand": "Lenovo"
>       }
>     }';

And i try show informations from kson code using this code bu i can´t get works

$rr=json_decode($fend);
foreach($rr->apollo as $result) {
    $lines=$result->data;


    foreach ($lines as $line) {

      echo $line->ProductItem;


        foreach ($line->ProductItem as $word) {
            echo($word->id . "\n");
        }
    }
}

I need extract data from json file and show informations from Uk for example, "en" and extract price but i think it´s all ok, but sure some question it´s bad, because i can´t get, the prblem

I try many combinations and don´t get any result, if you can tell me what´s wrong perfect, thank´s !!!!!

  • 1
    Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – aynber Aug 29 '19 at 14:34
  • `apollo` is an object, not an array. You won't be able to use foreach the way you think it will. Apollo has 1 data (object, not array), 1 ProductItem (object, not array). – aynber Aug 29 '19 at 14:35
  • Can you put some code for i can use for extract this informations and learn the way for this in this case with this json, thanks – Alfredo Aguin Figueiro Aug 29 '19 at 14:39
  • @AlfredoAguinFigueiro - Can you put the sample format you want to print. It will be helpful to understand what is the desired output. Accordingly will help u to code. – A. Todkar Aug 29 '19 at 14:45
  • I want extract the price for uk , you can see show country and under the price, i want show the price for uk or for each country, with example for this it´s perfect for me thank´s – Alfredo Aguin Figueiro Aug 29 '19 at 15:13

1 Answers1

0

Try this

foreach ($rr->apollo->data as $key => $value) {
        if(isset($value->price)){
            echo $value->price;
            echo "</br>";
        }
     }