PHP JSON解码到数组以获取特定的键值(所有字段名称相同) [英] PHP JSON Decoding To Array To Grab Specific Key Values (All fields same name)
本文介绍了PHP JSON解码到数组以获取特定的键值(所有字段名称相同)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从API URL中提取JSON数据.问题:在FL下有重复的键:"val"和"content".我只需要拉特定的键即可.
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.
如果我的格式不符合SO标准,请原谅.*
Please excuse me if my formatting is not correct to SO standards.*
我的问题发布在json和代码下方.
My question is posted below the json and code.
{
"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代码
$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("\n");
$entries = $entry['FL'];
foreach ($entries as $value) {
$val = $value['val'];
$content = $value['content'];
$out = $val." ".$content;
printf('%s'.PHP_EOL, $out);
printf("\n");
}
}
结果
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.
"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"
期望的输出
First Name Bobby
Last Name Bobby
Street 123 Fake Rd
City Fakecity
State FK
Zip Code 11111
将URL中的JSON设置为HTML表
推荐答案
只需编写代码即可完成您描述的事情.如果它们不在您想要的字段列表中,则只需不打印它们即可.
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("\n");
$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("\n");
}
}
}
这篇关于PHP JSON解码到数组以获取特定的键值(所有字段名称相同)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文