在复杂的PHP数组中搜索值

时间:2018-05-23 07:38:47

标签: php arrays json search

我已经阅读了许多关于从复杂数组中查找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

3 个答案:

答案 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可以是键或值 结果数组将包含两个子数组,如果它是键匹配或值匹配。

https://3v4l.org/05dm3