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条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作