如何在codeignigter中访问我的嵌套数组键?

时间:2017-06-20 08:47:29

标签: php codeigniter

"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"
                    }
                ]
            }

我的PHP代码

<?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; 
    } 
  } 
?>

我想要albumimages-&gt; imagepath

4 个答案:

答案 0 :(得分:0)

我认为你要找的是json_decode($ data,true);

这使得json数据成为可以使用的数组,如var_dump($data["albumimages"])

工作示例:https://3v4l.org/Q9lkW
并循环浏览您可以做的链接,https://3v4l.org/9rRSF

答案 1 :(得分:0)

数据采用json格式,因此使用&#39; json_encode()&#39;将数据转换为php对象。功能。此函数将json数据转换为php对象,使用php对象操作符访问该属性。

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

您可以使用foreach循环访问&#39; imagepath&#39;像这样

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

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

点击这里的工作示例 PHP sandbox

答案 2 :(得分:0)

使用此代码

$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/>";
            }
        }
    }
}

输出将是这样的

键: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

答案 3 :(得分: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;
    }
}