Reputation: 3972
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;
Upvotes: 0
Views: 155
Reputation: 43552
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"
Upvotes: 1