-1

I have gone through many posts on finding Value from Complex Array, but all of those only fetch up to second level. Can someone please provide me with code to recursively find the value for example state in address_line_2 whether exists or not?

I have the following 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

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • 2
    Can you edit your question and mention a sample input and output and what have you tried? – nice_dev May 23 '18 at 07:41
  • @vivek_23 Sample input can be any key or value in the array if I understand it correct. So just pick one. – Andreas May 23 '18 at 07:47
  • @Andreas So, the sample input happens to be a `key` instead of `value`, I return as is? – nice_dev May 23 '18 at 07:50
  • I guess you should output both key and value. But I'm not sure. – Andreas May 23 '18 at 07:54
  • 1
    Possible duplicate of [Search for a key in an array, recursively](https://stackoverflow.com/questions/3975585/search-for-a-key-in-an-array-recursively) – Progrock May 23 '18 at 08:05

3 Answers3

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'));

OUTPUT

string(38) "company=>address=>address_line_1=>city"
bool(true)
bool(false)

ASSUMPTIONS

Every key and value is unique in the array.

FUNCTION DEFINITION

The function getData() searches for a given string in the array.

  • If the string happens to be a key, it returns a boolean true
  • If the string happens to be a value in the array, it returns a key.
  • If string doesn't happen to be neither key nor value, it returns boolean false.
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

you can use array_column to get value of an given key

$array = json_decode($json,true);

$names = array_column($array,'name');

MGrahl
  • 11
  • 1
0

Instead of making it an array. What about parsing it as the string with regex?

$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 can be either a key or a value.
The result array will hold two subarrays with either if it's a key match or a value match.

https://3v4l.org/05dm3

Andreas
  • 23,610
  • 6
  • 30
  • 62