0

ARRAY 1:

array(1) {
  ["en"]=>
  array(1) {
    ["em"]=> null
  }
}

ARRAY 2 VALUES:

array(15) {
  ["something"]=>
  array(4) {
    ["somekey1"]=>
    string(25) "value1"
    ["somekey2"]=>
    string(9) "value2"
    ["somekey3"]=>
    string(5) "value3"
    ["somekey4"]=>
    string(3) "value4"
  }
  ["en"]=>
  array(3) {
    ["em"]=>
    string(4) "RESULT"

Look at array on ["en"]["em"] = "RESULT" from both arrays.

I want using $array1 and $array2 to intersecting a keys of array and get a result from $array2:

NOTE: On Array1 can be more nested arrays, ARRAY1 should find this keys on ARRAY2:

NOTE: I don't want to grab a data like $array2["en"]["em"], only using custom functions. (for example: custom array_intersect())

I have 2 arrays. Look at only keys. On Array1 there is en,em keys. I want these two keys to intersecting on Array2. When is intersecting with Array2, it will on Array2 get a value en,em->RESULT. I don't want a classical way to grab a data, just a COMPARE TWO ARRAYS and GET A VALUE.

I've tried intersecting, but this ONLY works if two arrays ARE SAME. So, I need to intersecting using nested recursive searching by key!

Example, I don't want:

$array2['en']['em'];

some_function_to_search_array_by_key(array $array2);

Example, that I WANT to:

Using function `array_intersect()` or some hardcoded sample.

get_result_by_two_arrays($array1, $array2);

Example of Result:

INPUT:

// search by arrays keys
$array1 = array('en' => 'em');

$result = get_result_by_two_arrays($array1, $array2);

RESULT:

$result = 
(string)RESULT;
8
  • so what have you tried so far Commented Aug 30, 2013 at 15:08
  • I've tried to grab a data from array2 using array_intersect() but it's strings problem. I want grabbing a data from array2 using compare keys and pick up a value from array2. Commented Aug 30, 2013 at 15:09
  • What do you want the result to look like? Commented Aug 30, 2013 at 15:12
  • Brate, kad bi mi tebe razumeli šta ti hoćeš ;) Like @RocketHazmat said, output example would be nice. Commented Aug 30, 2013 at 15:16
  • But $array1 = ['en' => ['em' => null]]; from top is not different from $array1 = ['en' => 'em']; in example. Commented Aug 30, 2013 at 15:33

1 Answer 1

1

Here is a simple example of what I think you want :

$array1 = ['en' => ['em' => null]];
$array2 = ['en' => ['em' => 'RESULT'], 'something' => ['somekey1' => 'somevalue1', 'somekey2' => 'somevalue2']];

function get_result_by_two_arrays(array $array1, array $array2) {
    if (!$array1) return;
    do {
        $key = current(array_keys($array1));
        $array1 = current($array1);
        if (!isset($array2[$key])) return;
        $array2 = $array2[$key];
    } while (is_array($array1));
    return $array2;
}

var_dump( get_result_by_two_arrays($array1, $array2) );
# string(6) "RESULT"
Sign up to request clarification or add additional context in comments.

2 Comments

You are AWESOME! Thank you! Works! It's working as full array and on one array! GREAT SNIPPET!
I think this can be useful snippet! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.