doujianqin5172 2019-04-04 15:59
浏览 264
已采纳

PHP JSON解码到数组以获取特定键值(所有字段同名)

I am pulling JSON data from an API URL. The problem: Under FL there are duplicate KEYS: "val" and "content". I only need to pull specific KEYS.

Please excuse me if my formatting is not correct to SO standards.*

My question is posted below the json and code.

JSON FROM URL

{
    "response": {
        "result": {
            "Leads": {
                "row": [
                    {
                        "no": "1",
                        "FL": [
                            {
                                "val": "LEADID",
                                "content": "123"
                            },
                            {
                                "val": "SMOWNERID",
                                "content": "3232"
                            },
                            {
                                "val": "Lead Owner",
                                "content": "Cassie"
                            },
                            {
                                "val": "First Name",
                                "content": "Bobby"
                            },
                            {
                                "val": "Last Name",
                                "content": "Something"
                            },
                            {
                                "val": "Email",
                                "content": "email@gmail.com"
                            },
                            {
                                "val": "Mobile",
                                "content": "1111111111"
                            },
                            {
                                "val": "SMCREATORID",
                                "content": "0000003213"
                            },
                            {
                                "val": "Created By",
                                "content": "Cassie"
                            },
                            {
                                "val": "Created Time",
                                "content": "2019-04-03 15:14:05"
                            },
                            {
                                "val": "Modified Time",
                                "content": "2019-04-03 17:13:58"
                            },
                            {
                                "val": "Full Name",
                                "content": "Bobby Something"
                            },
                            {
                                "val": "Street",
                                "content": "123 Fake Rd"
                            },
                            {
                                "val": "City",
                                "content": "Fakecity"
                            },
                            {
                                "val": "State",
                                "content": "FK"
                            },
                            {
                                "val": "Zip Code",
                                "content": "11111"
                            },
                            {
                                "val": "Email Opt Out",
                                "content": "false"
                            },
                            {
                                "val": "Salutation",
                                "content": "Mr."
                            },
                            {
                                "val": "Last Activity Time",
                                "content": "2019-04-03 17:13:58"
                            },
                            {
                                "val": "Tag",
                                "content": "Tag"
                            },
                            {
                                "val": "Account Name",
                                "content": "Something"
                            },
                            {
                                "val": "Territory Manager",
                                "content": "Michael Something"
                            },
                            {
                                "val": "Territory Manager_ID",
                                "content": "321237000000291111"
                            },
                            {
                                "val": "Classification",
                                "content": "Something"
                            },
                            {
                                "val": "Area",
                                "content": "Zone 1"
                            },
                            {
                                "val": "Account Number",
                                "content": "32345"
                            }
                        ]
                    }
                ]
            }
        },
        "uri": "/crm/private/json/Leads/getRecords"
    }
}

PHP CODE

$url = 'URL';
$data = file_get_contents($url);
$parsed = json_decode($data, true);

$eachEntry = $parsed['response']['result']['Leads']['row'];

foreach ($eachEntry as $entry) {

    $FL = $entry['no'];

    //printf('%s'.PHP_EOL, $FL);
    printf("
");

    $entries = $entry['FL'];

    foreach ($entries as $value) {

        $val = $value['val'];

        $content = $value['content'];

        $out = $val." ".$content;

        printf('%s'.PHP_EOL, $out);
        printf("
");
    } 

}

RESULTS

LEADID 123

SMOWNERID 3232

Lead Owner Cassie

First Name Bobby

Last Name Something

Email email@gmail.com

Mobile 1111111111

SMCREATORID 0000003213

Created By Cassie

Created Time 2019-04-03 15:14:05

Modified Time 2019-04-03 17:13:58

Full Name Bobby Something

Street 123 Fake Rd

City Fakecity

State FK

Zip Code 11111

Email Opt Out false

Salutation Mr.

Last Activity Time 2019-04-03 17:13:58

Tag Tag

Account Name Something

Territory Manager Michael Something

Territory Manager_ID 321237000000291111

Classification Something

Area Zone 1

Account Number 32345

QUESTION

How can I pull only the fields I have listed below in the example? And do not pull every field.

EXAMPLE

"val": "First Name",
"content": "Bobby"

,

"val": "Last Name",
"content": "Something"

,

"val": "Street",
"content": "123 Fake Rd"

,

"val": "City",
"content": "Fakecity"

,

"val": "State",
"content": "FK"

,

"val": "Zip Code",
"content": "11111"

EXPECTED OUTPUT

First Name Bobby

Last Name Bobby

Street 123 Fake Rd

City Fakecity

State FK

Zip Code 11111

Set JSON from URL into HTML Table

PHP JSON To Array Values Into HTML Table

  • 写回答

2条回答 默认 最新

  • doushaizhen1244 2019-04-04 16:06
    关注

    Just write the code to do what you describe. Simply don't print them if they're not in the list of fields you want.

    $valuesIWant = ["First Name", "Last Name", "Street", "City", "State", "Zip Code"];
    
    foreach ($eachEntry as $entry) {
    
        $FL = $entry['no'];
    
        //printf('%s'.PHP_EOL, $FL);
        printf("
    ");
    
        $entries = $entry['FL'];
    
        foreach ($entries as $value) {
    
            $val = $value['val'];
    
            $content = $value['content'];
    
            if (in_array($val, $valuesIWant)) {
                $out = $val." ".$content;
    
                printf('%s'.PHP_EOL, $out);
                printf("
    ");
            }
    
        } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证