douchao1879
2018-02-27 23:19
浏览 101

我如何使用php中的数组访问Json文件中的数据并回显到html文件

Hi I have an json file that include a field of array named urlPhotos. I would like to have a list for urlPhotos om my html page using the echo method in PHP

my data in the json file has this structure

[{
    "caseKey": "4846ccde-8e2c-4360-9381-9d78c12b1ef6",
    "caseNumber": "177",
    "companyName": "Bach",
    "officeId": 37,
    "urlPhotos": [
        "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
        "https://boligsystem.flex.dk/public/520-80276a.jpg", 
        "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
        "https://boligsystem.flex.dk/public/569f86636fd8.jpg"           
    ],

    "timestamp": "09022018073555"
}
{
    "caseKey": "4846ccde-8e2c-4360-9381-9d78c12baaa",
    "caseNumber": "199",
    "companyName": "Bach",
    "officeId": 37,
    "urlPhotos": [      
        "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
        "https://boligsystem.flex.dk/public/520-80276a.jpg", 
        "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
        "https://boligsystem.flex.dk/public/569f86636fd8.jpg"
        "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
        "https://boligsystem.flex.dk/public/520-80276a.jpg", 
        "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
        "https://boligsystem.flex.dk/public/569f86636fd8.jpg"       
        ],
    "timestamp": "11022018073555"
}]

图片转代码服务由CSDN问答提供 功能建议

您好我有一个json文件,其中包含一个名为urlPhotos的数组字段。 我希望在PHP中使用echo方法获取urlPhotos om html页面列表

我的json文件中的数据具有此结构

   [{
“caseKey”:“4846ccde-8e2c-4360-9381-9d78c12b1ef6”,
“caseNumber”:“177”,
“companyName”:“Bach”,
“officeId”:37  ,
“urlPhotos”:[
“https://boligsystem.flex.dk/public/569f868e-9.jpg”,
“https://boligsystem.flex.dk/public/520-80276a.jpg  “,
”https://boligsystem.flex.dk/public/569f86763c9.jpg“,
”https://boligsystem.flex.dk/public/569f86636fd8.jpg“
],
 
”  timestamp“:”09022018073555“
} 
 {
”caseKey“:”4846ccde-8e2c-4360-9381-9d78c12baaa“,
”caseNumber“:”199“,
”companyName“:”Bach“,  
“officeId”:37,
“urlPhotos”:[
“https://boligsystem.flex.dk/public/569f868e-9.jpg”,
“https://boligsystem.flex.dk/  public / 520-80276a.jpg“,
”https://boligsystem.flex.dk/public/569f86763c9.jpg“,
”https:// boligsys  tem.flex.dk/public/569f86636fd8.jpg"
“https://boligsystem.flex.dk/public/569f868e-9.jpg”,
“https://boligsystem.flex.dk/public/520  -80276a.jpg“,
”https://boligsystem.flex.dk/public/569f86763c9.jpg“,
”https://boligsystem.flex.dk/public/569f86636fd8.jpg“
],\  n“timestamp”:“11022018073555”
}] 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongzha3058 2018-02-27 23:28
    已采纳

    You can decode the json array with json_decode to normal php array and then use the foreach on this array to get urlPhotos like

    foreach($arr['urlPhotos'] as $t)
    {
        echo $t
    }
    

    And you can add list marks to this foreach

    已采纳该答案
    打赏 评论
  • doutan3040 2018-02-27 23:34

    First of all, you must fetch the json file contents into PHP. One way of doing it:

    $contents = file_get_contents(<your-file-name>);

    Then you must convert this raw JSON string into a JSON object. As the json is an array, you will get an array:

    $jsonArray = json_decode($contents);

    Then looping through this array is how you reach the urlPhotos:

    foreach($jsonArray as $o){
      foreach($o->urlPhotos as $photo){
        echo $photo . "<br>";    
      }
    }
    

    This code above would loop the array and grab every object. Then within each object you have a urlPhotos property, which is an array that you can use in any way you want. In this case we are looping through every photo and as you commented above we are echoing its contents via php echo.

    打赏 评论
  • doushan3511 2018-02-28 02:36

    See this script below . I have used pure javascript along with Jquery.But you can use only javascript.

    Your data was not well formatted , it was missing comma between each main curly brace, and a comma in urlPhotos array

    $(document).ready(function(){
           $("#clickme").click(function(){
              getPhotoUri(MyUris);
            }); 
        });
    
    var MyUris = [
        {
           "caseKey": "4846ccde-8e2c-4360-9381-9d78c12b1ef6",
           "caseNumber": "177",
           "companyName": "Bach",
           "officeId": 37,
           "urlPhotos": [
               "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
               "https://boligsystem.flex.dk/public/520-80276a.jpg", 
               "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
               "https://boligsystem.flex.dk/public/569f86636fd8.jpg"           
           ],
         "timestamp": "09022018073555"
        }, //here you did not put a comma
    
       {
         "caseKey": "4846ccde-8e2c-4360-9381-9d78c12baaa",
         "caseNumber": "199",
         "companyName": "Bach",
         "officeId": 37,
         "urlPhotos": [      
            "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
            "https://boligsystem.flex.dk/public/520-80276a.jpg", 
            "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
            "https://boligsystem.flex.dk/public/569f86636fd8.jpg",
            //here you did not put a comma 
            "https://boligsystem.flex.dk/public/569f868e-9.jpg", 
            "https://boligsystem.flex.dk/public/520-80276a.jpg", 
            "https://boligsystem.flex.dk/public/569f86763c9.jpg", 
            "https://boligsystem.flex.dk/public/569f86636fd8.jpg"           
            ],
    
        "timestamp": "11022018073555"
       }
    ]
    
    function getPhotoUri(arr){
        for(i=0; i <arr.length; i++ ){
            for(y=0; y < arr[i].urlPhotos.length; y++){
                $("#urlphoto").append(arr[i].urlPhotos[y] + "<br> ");
            }
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    
    <button id="clickme">Get your photo</button> 
        <h2> LIST OF YOUR URL PHOTO.</h2>
        <p id="urlphoto"> </p>

    </div>
    
    打赏 评论

相关推荐 更多相似问题