K
K
kanonir082021-09-10 22:43:03
PHP
kanonir08, 2021-09-10 22:43:03

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;
}


As a result, we get the following data array

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 )


How do I get the [Price] of a specific single row of an array?

For example, I need to get the value of [Price] from [ID] => 5 (0.04831000) into the $bts variable. How to do this please tell me. I tried 10 codes in ready-made answers, but nothing works for me as it should.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dzmitryIhnatau, 2021-09-11
@dzmitryIhnatau

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 question

Ask a Question

731 491 924 answers to any question