Answer the question
In order to leave comments, you need to log in
How to get a specific array element and send it to a function?
I have an array that we get from the database with the following code
$sql = "SELECT * FROM cryptodb";
if($result = $conn->query($sql)){
foreach($result as $row){
print_r ($row);
echo "<br/>";
}
$result->free();
} else{
echo "Ошибка: " . $conn->error;
}
Array ( [ID] => 1 [Name] => BTC [Price] => 45666 )
Array ( [ID] => 2 [Name] => ETH [Price] => 3325 )
Array ( [ID] => 3 [Name] => ADA [Price] => 2.41400000 )
Array ( [ID] => 4 [Name] => CHZ [Price] => 0.33660000 )
Array ( [ID] => 5 [Name] => BTS [Price] => 0.04831000 )
Array ( [ID] => 6 [Name] => C98 [Price] => 3.71600000 )
Array ( [ID] => 7 [Name] => REQ [Price] => 0.20950000 )
Array ( [ID] => 8 [Name] => EPS [Price] => 0.64290000 )
Array ( [ID] => 9 [Name] => DATA [Price] => 0.12509000 )
Array ( [ID] => 10 [Name] => MBOX [Price] => 5.57800000 )
Array ( [ID] => 11 [Name] => TRIBE [Price] => 0.62760000 )
Array ( [ID] => 12 [Name] => ALICE [Price] => 12.49000000 )
Array ( [ID] => 13 [Name] => NEAR [Price] => 9.02300000 )
Answer the question
In order to leave comments, you need to log in
Maybe I missed something, but you can first create a variable before the loop, for example
A in the loop, do this:
Then after the loop you can get the value by id:
You can also create a function where you will pass the id you need through the argument and get everything through sql:
Don't forget case :) $crypt = array()
$crypt[row['id']] = $row['price'];
echo $crypr[5];
$sql = "SELECT * FROM cryptodb WHERE id = {$id}";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question