dongshuql24533 2016-09-05 14:05
浏览 44
已采纳

在PHP中解析JSON url

I have a json data that I want to get it from my json array so that I can display the data on my php form. Here is my json object:

{
"response": {
    "result": {
        "Contacts": {
            "row": [
                {
                    "no": "1",
                    "fl": [
                        {
                            "val": "CONTACTID",
                            "content": "144120000000079041"
                        },
                        {
                            "val": "First Name",
                            "content": "Tata"
                        },
                        {
                            "val": "Last Name",
                            "content": "Nyerere"
                        },
                        {
                            "val": "Email",
                            "content": "nyereredaniel@gmail.com"
                        },
                        {
                            "val": "Account Name",
                            "content": "null"
                        },
                        {
                            "val": "Phone",
                            "content": "null"
                        },
                        {
                            "val": "Mobile",
                            "content": "null"
                        }
                    ]
                },
                {
                    "no": "2",
                    "fl": [
                        {
                            "val": "CONTACTID",
                            "content": "144120000000069001"
                        },
                        {
                            "val": "First Name",
                            "content": "null"
                        },
                        {
                            "val": "Last Name",
                            "content": "DANIEL NYERERE"
                        },
                        {
                            "val": "Email",
                            "content": "nyereredaniel@hotmail.com"
                        },
                        {
                            "val": "Account Name",
                            "content": "null"
                        },
                        {
                            "val": "Phone",
                            "content": "null"
                        },
                        {
                            "val": "Mobile",
                            "content": "null"
                        }
                    ]
                },
                {
                    "no": "3",
                    "fl": [
                        {
                            "val": "CONTACTID",
                            "content": "144120000000065068"
                        },
                        {
                            "val": "First Name",
                            "content": "null"
                        },
                        {
                            "val": "Last Name",
                            "content": "Lawrence"
                        },
                        {
                            "val": "Email",
                            "content": "support@zohosupport.com"
                        },
                        {
                            "val": "ACCOUNTID",
                            "content": "144120000000065066"
                        },
                        {
                            "val": "Account Name",
                            "content": "Zoho"
                        },
                        {
                            "val": "Phone",
                            "content": "1 888 900 9646"
                        },
                        {
                            "val": "Mobile",
                            "content": "null"
                        }
                    ]
                }
            ]
        }
    },
    "uri": "/api/json/contacts/getrecords"
}
}

I need to display those data on my php form , and here is the sample i have tried but i'm stuck with it:`

<?php
$url = "my url";
$json = file_get_contents($url);
$json = json_decode($json, true);
$response = $json['response'];
$result = $response['result'];
$contact = $result['Contacts'];
$data = $contact['row'];
$name = $dat[0];
echo "Contact name: " . $name;
?>

I want to display the values of CONTACTID ,First Name ,Last Name ,Email into my php form Anyone who can assist on that ,will appreciate a lot

  • 写回答

2条回答 默认 最新

  • doujing9972 2016-09-05 14:16
    关注

    Here is simple way, i made a helper function to extract value from array. Look at this example:

    <?php
    $url      = "my url";
    $json     = file_get_contents($url);
    $json     = json_decode($json, true);
    $response = $json['response'];
    $result   = $response['result'];
    $contact  = $result['Contacts'];
    $data     = $contact['row'];
    
    // Helper Function
    function getValByName($name, $array)
    {
        foreach ($array as $row) {
            if ($row['val'] == $name) {
                return $row['content'];
            }
        }
    
        return '';
    }
    
    foreach ($data as $row) {
        $FirstName = getValByName('First Name', $row['fl']);
        $ContactId = getValByName('CONTACTID', $row['fl']);
        $LastName  = getValByName('Last Name', $row['fl']);
        $Email     = getValByName('Email', $row['fl']);
        $Phone     = getValByName('Phone', $row['fl']);
        $Mobile    = getValByName('Mobile', $row['fl']);
    
        echo $FirstName . ' ' . $LastName . '<br/>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况