tazo todua tazo todua - 1 year ago 76
PHP Question

Find ARRAY by to its values

I have a multi-dimensional array like so:

$a = array(
'potatoe'=> array(


'apple'=> array(

I am looking for a way to searching the fruit name (with its values) when I only know weight is
and year is
. How to do that?

Answer Source

You will have to iterate over it using foreach and test every element.

function findFruit($array,$weight, $year){
    foreach($array as $key => $fruit){
        if($fruit['weight'] == $weight && $fruit['year'] == $year ){
            return $key;

then, just use the function:

$theFruit = $a[findFruit($a,55,1992)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download