Get the position of an associative Array inside another Array

0

I have this array's associative array:

Array { 

    [0]=> array(4) { ["nome"]=> string(35) "ACRILEX TECIDO 37ML AMARELO CANARIO" 
                     ["preco"]=> string(4) "1.65" 
                     ["quantidade"]=> string(1) "1" 
                     ["imagem"]=> string(25) "Loja/imgPQ/4140589589.gif" } 

    [1]=> array(4) { ["nome"]=> string(35) "ACRILEX TECIDO 37ML AMARELO LIM�O" 
                     ["preco"]=> string(4) "1.65" 
                     ["quantidade"]=> string(1) "1" 
                     ["imagem"]=> string(25) "Loja/imgPQ/4140504504.gif" } }

And I wanted you run and when he found the same name to one of the products return the position where this associative array and then have access to your quantity

    
asked by anonymous 29.09.2017 / 10:34

1 answer

0

The simplest way to solve the problem is to go through the array by checking the name of each position and, when you find the name you want, return it.

function getKeyFromName($array, $name) {
    foreach($array as $key => $value) {
        if ($value["nome"] == $name) {
            return $key;
        }
    }

    return false;
}

See working at Ideone | Repl.it

An alternative is to use the array_column and array_search together:

function getKeyFromName($array, $name) {
    return array_search($name, array_column($array, "nome"));
}

See working at Ideone | Repl.it

The result will be exactly the same.

    
29.09.2017 / 12:12