0

Good day everyone.

I have an regular array (this is the print_r result, the array can have from 1 to n positions):

Array
(
    [1] => value1
    [2] => value2
    [3] => value3
)

I have another array defined elsewhere as:

$array_def['value1']['value2']['value3'] = array(
 'fl' => 'field1',
 'f2' => 'field2',
);

Using the first array result, how can i check if $array_def exists? In other words, i need to use a flat array values to check if a multidimensional array correspondence exists; keep in mind that the values can repeat in the first array, therefore flipping values with keys it's not an option as it will collide and remove duplicated values.

Thanks in advance.

2 Answers 2

1

You can do it this way:

$a = array(1=>'value1', 2=>'value2', 3=>'value3');
$array_def[$a[1]][$a[2]][$a[3]] = array(
 'fl' => 'field1',
 'f2' => 'field2',
);

I don't think there's any shortcut or special built-in function to do this.

Sign up to request clarification or add additional context in comments.

1 Comment

This is assuming $a will always have 3 elements and $array_def too, i mean, this is a flat solution, not a dynamic one, thanks for the effort though =)
0

Found the perfect function for you. returns not only exists, but position within a multi-dimensional array..

http://www.php.net/manual/en/function.array-search.php#47116

dated: 03-Nov-2004 11:13 too much to copy/paste

you can then loop over your flat array and foreach:

multi_array_search($search_value, $the_array)

2 Comments

This seems to be kinda the opposite of what the OP was looking for.
Exactly, i need to search keys in array A using values from array B, this returns keys using a value, effort appreciated though

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.