Find value within an array

Asked

Viewed 1,128 times

3

I have the following array:

    array (size=80)
    0 => 
    array (size=2) 
        ‘cod_produto' => int 107 
        'valor' => float 20 
    1 =>
       
    array (size=2) 
        ‘cod_produto' => int 109 
        'valor' => float 375.8 
    2 => 
    array (size=2) 
        ‘cod_produto' => int 112 
        'valor' => float 20

I’m riding him like this:

Loop {

    // Monta array
    $valores[] = array(
        "cod_produto" => (int) $resultado_precos-> cod_produto,
        "valor" => (float) $resultado_precos->valor
    );
}

The array is in a variable $valores, I need to take back the value of the product in a report, but I have only the cod_produto.

I need to search in the array for the product value and if the value does not exist I have to display the value as zero.

In short, I have the product code and need to locate its price in this array.

  • 1

    I believe the in_array function of PHP suits you. Check out the documentation: http://php.net/manual/en/function.in-array.php

  • What do you mean "take back the value of the product"? You have the product code and want to search in this array what is the associated value?

  • Better yet, the array_search function. Doc: http://php.net/manual/en/function.array-search.php It would look like this: $Cod = array_search($search, $value);

  • @Andersoncarloswoss that’s right, I have the product code and I need to locate its price in this array, sorry I wasn’t clear with my question.

2 answers

4


I would do so:

$valores = array (

    0 => 
    array(
        'cod_produto' => 107,
        'valor' => 20 
    ),

    1 =>     
    array (
        'cod_produto' => 109 ,
        'valor' => 375.8 
    ),

    2 => 
    array(
        'cod_produto' => 112 ,
        'valor' => 20
    )

);

$codProcura = 112;
$valor = 0;

for($x = 0; $x < count($valores); $x++){    
    $search = $valores[$x];
    if($search['cod_produto'] == $codProcura){
        $valor = $search['valor'];
        break;
    }
}

echo $valor;

Look at the ideone

There is another way using the array_search, as quoted by @Vitorandre, along with array_colum.

Thus:

$valor = 0;
$local = array_search(112, array_column($valores, 'cod_produto'));
$valor = $valores[$local]['valor'];
echo $valor;
  • In terms of performance there is some difference between them?

  • @Hugoborges I had THOUGHT the for it was faster. But I did some tests and it doesn’t change much no..

  • 1

    Okay, thank you very much.

2

You can also use a function with foreach:

$valores = array(
   array('cod_produto' => 101,'valor' => 200),
   array('cod_produto' => 102,'valor' => 300),
   array('cod_produto' => 103,'valor' => 400)
);

$buscar = 102; // código do produto a buscar

function encontrar($array, $chave, $valor){
   foreach($array as $key => $value){
      if($value[$chave] == $valor){
         return $array[$key]['valor'];
      }
   }
}

$resultado = encontrar($valores, 'cod_produto', $buscar);
$cod_produto = $resultado ? $resultado : '0';
echo $cod_produto; // retorna 300

Check it out at Ideone

  • True! The old man foreach.. that I always forget!

  • 2

    Rss... but your response is great!

  • 1

    Thanks dvd! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.