0
"data": [
            {
                "pid": "81",
                "fname": "Parth",
                "lname": "Tandel",
                "pfname": "Parth",
                "plname": "Tandel",
                "userprofilephoto": "/Images/ProfilePictures/18/DSC_0164.JPG",
                "parentprofilephoto": "/Images/ProfilePictures/18/DSC_0164.JPG",
                "type": "ALBUM",
                "likescount": "1",
                "commentscount": "1",
                "sharecount": "0",
                "sharepid": null,
                "uaid": "18",
                "ownerid": "18",
                "parentid": null,
                "title": "newalbum2",
                "description": "",
                "sharedescription": null,
                "imagepath": null,
                "previewurl": null,
                "previewtitle": null,
                "previewshortdescription": null,
                "previewimageurl": null,
                "createdon": "2017-05-29 15:44:04",
                "posttype": "5",
                "comments": [
                    {
                        "pcid": "21",
                        "uaid": "31",
                        "comment": "this is dope",
                        "fname": "maulik",
                        "lname": "kanani",
                        "profPicturepath": "https://www.gravatar.com/avatar/003dbb32079ee5ff19ed75476f562bd1",
                        "createdon": "2017-06-15 23:50:36"
                    }
                ],
                "albumimages": [
                    {
                        "imagepath": "/Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-36.png"
                    },
                    {
                        "imagepath": "/Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-361.png"
                    },
                    {
                        "imagepath": "/Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-363.png"
                    },
                    {
                        "imagepath": "/Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-364.png"
                    },
                    {
                        "imagepath": "/Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-365.png"
                    }
                ]
            }

My PHP code

<?php 
  for ($i=0; $i < sizeof($value->albumimages); $i++) 
  { 
    $x = count($value->albumimages); 
    switch($x) 
    { 
      break; 
      default: 
        if($i == 0 || $i == 1) 
        { 
          echo '<div class="col-sm-6 pads5 marb10"> <img class="full" src="'.getapiPath().$imgs->imagepath.'"> </div>'; 
        } 
        break; 
    } 
  } 
?>

I want albumimages->imagepath

Toby
  • 9,696
  • 16
  • 68
  • 132
Maulik Kanani
  • 69
  • 1
  • 2
  • 11

4 Answers4

0

I think what you are looking for is json_decode($data, true);

This makes the json data an array that you can use like var_dump($data["albumimages"])

Working example: https://3v4l.org/Q9lkW
And to loop through the links you can do foreach, https://3v4l.org/9rRSF

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

The data is in json format so convert the data into php object by using 'json_encode()' function. This function converts the json data into php object the access the property by using php object operator.

$data = json_encode('your_json_string');
//and access like this
$data[0]->albumimages

You can use the foreach loop to access the 'imagepath' like this

//get the albumsimages
$albumimages = $data[0]->albumimages;

//then use foreach to access the imagepath like this
foreach($albumimages as $image) {
    echo $image->imagepath ."\n";
}

Check out working example here PHP sandbox

Saurabh
  • 71
  • 7
0

Use this code

$a = json_decode('YOUR JSON STRING',true);
foreach($a['data'][0] as $key => $value){
    if($key == 'albumimages'){
        for($i = 0; $i < count($value); $i++){
            foreach($value[$i] as $k => $v){
                echo "Key: ".$k." Value: ".$v."<br/>";
            }
        }
    }
}

Output will be like this

Key: imagepath Value: /Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-36.png

Key: imagepath Value: /Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-361.png

Key: imagepath Value: /Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-363.png

Key: imagepath Value: /Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-364.png

Key: imagepath Value: /Images/18/Albums/newalbum2/Screenshot_from_2017-06-12_15-11-365.png

Akshay Singhai
  • 189
  • 2
  • 10
0
for ($i=0; $i < sizeof($value->albumimages); $i++) {
    $x = count($value->albumimages);
    switch($x) {
        default: 
            if($i == 0 || $i == 1) 
            { 
              echo '<div class="col-sm-6 pads5 marb10"> <img class="full" src="'.getapiPath().$value->albumimages[$i]->imagepath.'"> </div>'; 
            } 
            break;
    }
}
Maulik Kanani
  • 69
  • 1
  • 2
  • 11