Using array values as another array keys

154 views Asked by At

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

There are 2 answers

1
Bill Karwin On

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.

2
FatherStorm On

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)