doujianqin5172
doujianqin5172
2019-04-04 15:59

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 doushaizhen1244 2年前

    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("
    ");
            }
    
        } 
    }
    
    点赞 评论 复制链接分享
  • doushan1157 doushan1157 2年前

    you just need to maintain an array where you can provide all the options you want to fetch and check that in the loop like in the example below.

    $url = 'URL';
    $data = file_get_contents($url);
    $parsed = json_decode($data, true);
    
    $eachEntry = $parsed['response']['result']['Leads']['row'];
    
    $required = ['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) {
    
            if(!in_array($value['val'],$required))
                continue;
    
            $val = $value['val'];
    
            $content = $value['content'];
    
            $out = $val." ".$content;
    
            printf('%s'.PHP_EOL, $out);
            printf("
    ");
       } 
    
    }
    
    点赞 评论 复制链接分享

相关推荐