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.
I believe the in_array function of PHP suits you. Check out the documentation: http://php.net/manual/en/function.in-array.php
– Vítor André
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?
– Woss
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);
– Vítor André
@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.
– Hugo Borges