0

Hello everyone, my question is I have a JSON string row like:-

{id":15,"description":"new product","pic1":"15682078931.jpg","pic2":"15682078632.jpg","pic3":"15682078633.jpg","pic4":"15682078634.png","pic5":"15682078635.jpg"}

And I want to print like:-

$i = 1;
foreach($product as products)
{
   $i++;
  $pics = $product->pic.[$i];
  echo $pics;
}

how could I do, I didn't understand.

  • 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) – ascsoftw Oct 05 '19 at 06:34

2 Answers2

0

You have a wrong JSON string. You were missing double quote " before id

<?php 
$json = '{"id":15,"description":"new product","pic1":"15682078931.jpg","pic2":"15682078632.jpg","pic3":"15682078633.jpg","pic4":"15682078634.png","pic5":"15682078635.jpg"}';


$product = json_decode($json);

echo $product->pic1;
 ?>

Here is the demo

Zain Farooq
  • 2,956
  • 3
  • 20
  • 42
0

This is the solution to the problem I solved

 for($i=1; $i<=5; $i++)    
    {
         $pic = 'pic'.$i;
         $new .= '<div class="item mt-5 mx-1">
                      <div class="z-depth-2 mb-2 mx-height">
                        <img class="round-top" src=" asset('/images/logo/'.$arrivals[$pic])" alt="Card image cap">
                    </div>
                  </div>';
            // echo $arrivals[$pic];
        }

This problem is solved but the new problem is how to append an image in JSON like:

<img class="round-top" src=" asset('/images/logo/'.$arrivals[$pic])" alt="Card image cap">

.It shows this type of error:-

Use of undefined constant images - assumed 'images' (this will throw an Error in a future version of PHP)