1

I am using a API to get deals from a shopping website and I get the following JSON response -

{
    "update_time": "2020-07-23 22:01:31",
    "offers": [{
        "products": [{
            "title": "Product Name",
            "endDate": "2020-07-24 03:55:01",
            "url": "https://store.com/productid",
            "offerPercent": 87,
            "offerPrice": "12.74",
            "id": "3cbfdc55",
            "normalPrice": "99.99",
            "reviewRating": 4.7428455,
            "imageUrl": "https://store.com/productid/image",
            "totalReviews": "1856"
        }, { //This repeats alot of times

I am tried to use PHP to:

1 Retrieve the data into PHP

2 Loop through all of the items, storing all of the parameters, like url, title etc.

3 Concatenate that with some html (like a div tag) e.g:

$html = '';

$html = $html . '<div>Title' . $title .'</div>'; //Repeating this for each item?

Is that possible, if so, how? Could you point me in the right direction?

Edit: I used json_decode to convert the json to php ($someArray json_decode($jsonlink, true); then I used foreach to read the contents

foreach ($someArray as $key => $value) {
    echo $value[1][1];
}

And the webpage showed up as blank. What am I doing wrong.

3 Answers 3

1

Yes you can convert your json object to php structure by using json_decode in php, this will transform the json to a php array like this.

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
json_decode($json)

The output will be like this.

object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)

}

After that you have to use a recursive function or a foreach to read the object and then get and print the information that you need.

You can read this info here PHP json_decode PHP foreach

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, there was a misunderstanding. I forgot to write that I did use json_decode and for each and it didn't work. I'll edit my post with more info
Ok, I think the answer below explains better what you need :)
1

After decoding your JSON using json_decode you can loop through the items like this (using provided code as example):

// $raw_json would be the json you received
$data = json_decode($raw_json);

$html = "";
foreach($data->offers as $offer){
    // $offer now has all of the child properties e.g. $offer->products
    foreach($offer->products as $product){
        // $product now has all of the child properties e.g. $product->title
        $html .= "<div>Title: {$product->title}</div>";
    }

}

json_decode has a second parameter which you can pass true to ensure it returns an Associative Array meaning you can access properties like $variable["propName"]. This would change the above code to:

// $raw_json would be the json you received
$data = json_decode($raw_json, true);

$html = "";
foreach($data['offers'] as $offer){
    // $offer now has all of the child properties e.g. $offer['products'[
    foreach($offer['products ']as $product){
        // $product now has all of the child properties e.g. $product['title']
        $html .= "<div>Title: {$product['title']}</div>";
    }
}

8 Comments

Thank you. One problem: The element offers has the child element products and in that there is a sub propert(ies) of different items (e.g 1,2) and in those items (e.g item 1) there is description and price etc. How do I access it then?
@hopefulhacker-ReinstateMonica I've updated the answer - you can use the same approach again inside the $offers loop (i.e. create a $products loop)
when I used the following code $jsonlink = file_get_contents('externaljson.txt'); // $raw_json would be the json you received $data = json_decode($jsonlink, true); $html = ""; foreach($data['offers'] as $offer){ // $offer now has all of the child properties e.g. $offer['products'[ foreach($offer['products ']as $product){ // $product now has all of the child properties e.g. $product['title'] $html .= "<div>Title: ". $product["description"] ."</div>"; } } the webpage showed up blank what did I do wrong
do you want the json?
@hopefulhacker-ReinstateMonica Did you echo the $html variable?
|
1

You need to loop inside the array that conatains the data you want.

$data = json_decode($raw_json);
foreach ($data['offers']['products] as $product) {
    echo $product['title'];
}

this is how you display the data on your website.

If you want to display your data with html and css styles:

  1. First what i do is copy the html component like a bootstrap card, row, col, etc.
  2. then paste it on a variable
$html = '<div>
<h1>here goes my div</h1>
<img src="here/goes/your/url.png" />
<p>Description</p>
</div>';
  1. Then, replace the dummy data with your own data from the foreach array:
$data = json_decode($raw_json);
 foreach ($data['offers']['products'] as $product) {
  $html = '<div>
  <h1>'.$product['title'].'</h1>
  <img src="'.$product['imageUrl'].'" />
  <p>'.$product['normalPrice'].'</p>
  </div>';
 }
  1. Finally use echo to render the component
$data = json_decode($raw_json);
 foreach ($data['offers']['products'] as $product) {
  $html = '<div>
  <h1>'.$product['title'].'</h1>
  <img src="'.$product['imageUrl'].'" />
  <p>'.$product['normalPrice'].'</p>
  </div>';
 echo $html;
 }

3 Comments

sure, it may be the data strucuture that doesnt match the foreach loop
k. the raw json is here: pastebin.com/raw/g6Ek2wLD
it is just a nested foreach what you need, this is an example: example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.