我已经阅读了许多关于从复杂数组中查找Value的帖子,但所有这些帖子只能获取到第二级。有人可以请我提供代码,以递归方式找到address_line_2中的状态值是否存在?
我有以下JSON
{
"company": {
"id": "123456",
"name": "Test Company LLC.",
"FEIN": "22-2222222",
"address": {
"address_line_1": {
"street": "1 Street St, Suite 12",
"city": "San Francisco",
"postalCode": "123456"
},
"address_line_2": {
"state": "CA",
"country": "USA"
}
},
"phone": "1-800-XXX-XXXX",
"fax": "1-800-XXX-XXXX",
"email": "admin@abc.com",
"URL": "www.abc.com"
},
"producer": {
"id": "LLXXXX",
"name": "Test Name",
"phone": "555-555-5555",
"mobile": "555-555-5555",
"fax": "555-555-5555",
"email": "test@abc.com",
"producerSubCode": "111",
"NIPRId": "123456",
"stateProducerId": "12344"
}
}
〜Harshit
答案 0 :(得分:1)
<?php
$json_string = '{"company":{"id":"123456","name":"Test Company LLC.","FEIN":"22-2222222","address":{"address_line_1":{"street":"1 Street St, Suite 12","city":"San Francisco","postalCode":"123456"},"address_line_2":{"state":"CA","country":"USA"}},"phone":"1-800-XXX-XXXX","fax":"1-800-XXX-XXXX","email":"admin@abc.com","URL":"www.abc.com"},"producer":{"id":"LLXXXX","name":"Test Name","phone":"555-555-5555","mobile":"555-555-5555","fax":"555-555-5555","email":"test@abc.com","producerSubCode":"111","NIPRId":"123456","stateProducerId":"12344"}}';
$json_array = json_decode($json_string,true);
function getData($json_array,$search_value){
foreach($json_array as $each_key => $each_value){
if($each_key === $search_value) return true;
if(is_array($each_value)){
$return_value = getData($each_value,$search_value);
if($return_value !== false) return $return_value === true ? $return_value : $each_key."=>".$return_value;
}else if($each_value === $search_value){
return $each_key;
}
}
return false;
}
echo "<pre>";
var_dump(getData($json_array,'San Francisco'));
var_dump(getData($json_array,'producerSubCode'));
var_dump(getData($json_array,'abc123'));
<强>输出强>
string(38) "company=>address=>address_line_1=>city"
bool(true)
bool(false)
<强>的假设强>
每个键和值在数组中都是唯一的。
功能定义
函数getData()
搜索数组中的给定字符串。
key
,则返回布尔值true
value
,则会返回key
。key
也不是value
,则返回布尔值false
。答案 1 :(得分:0)
您可以使用array_column获取给定密钥的值
$ array = json_decode($ json,true);
$ names = array_column($ array,&#39; name&#39;);
答案 2 :(得分:0)
而不是使它成为一个数组。用正则表达式解析它作为字符串怎么样?
$find = "San Francisco";
$re = '/(' . $find . ')\":\s\"(.*?)\"|\"(\w+)\":\s\"(' . $find . ')/m';
$str = '{ "company": { "id": "123456", "name": "Test Company LLC.", "FEIN": "22-2222222", "address": { "address_line_1": { "street": "1 Street St, Suite 12", "city": "San Francisco", "postalCode": "123456" }, "address_line_2": { "state": "CA", "country": "USA" } }, "phone": "1-800-XXX-XXXX", "fax": "1-800-XXX-XXXX", "email": "admin@abc.com", "URL": "www.abc.com" }, "producer": { "id": "LLXXXX", "name": "Test Name", "phone": "555-555-5555", "mobile": "555-555-5555", "fax": "555-555-5555", "email": "test@abc.com", "producerSubCode": "111", "NIPRId": "123456", "stateProducerId": "12344" } }';
preg_match_all($re, $str, $matches);
$result["key-match"] = array_slice(array_column($matches, 0),1,2);
$result["value-match"] = array_slice(array_column($matches, 0),3,2);
var_dump($result);
$find
可以是键或值
结果数组将包含两个子数组,如果它是键匹配或值匹配。