up vote 0 down vote favorite

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.

link|flag

40% accept rate

2 Answers

up vote 1 down vote

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.

link|flag
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 =) – Kusanagi2k yesterday
up vote 0 down vote

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)

link|flag
This seems to be kinda the opposite of what the OP was looking for. – cHao Oct 8 at 22:30
Exactly, i need to search keys in array A using values from array B, this returns keys using a value, effort appreciated though – Kusanagi2k yesterday

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.