Recover php array data via index value

Asked

Viewed 580 times

0

My array in print_r displays like this:

[
    {
        "id": "A220", 
        "name": "Dipirona", 
        "symbol": "R$", 
        "rank": "1", 
        "price_br": "8.4", 
        "price_usd": "2.0"
        "total_supply": "287.0", 
        "max_supply": "21000.0", 
        "last_updated": "1519152868"
    }, 
    {
        "id": "A220", 
        "name": "Eno", 
        "symbol": "R$", 
        "rank": "3", 
        "price_br": "2.4", 
        "price_usd": "1.0"
        "total_supply": "341.0", 
        "max_supply": "1200.0", 
        "last_updated": "1615122869"
    }
]

How to recover the ID values: A220, for example, which are the product data "Dipirona", and store in another array only the data of the "Dipirona"" ?

2 answers

1

Just use a forach to read the array Suppose your array is called $arr

<?php
    foreach($arr as $prod){
        echo $prod['id']
        // ai é so chamar a variavel $prod com o indice
    }
?>

Or if you want to access directly without loop

$arr[0]['id']; 
$arr[1]['id'];
  • Failed, error appeared: Warning: Illegal string offset 'id' in C: xampp htdocs testes index.php on line 59 h foreach($Remedies as $remedio){ echo $remedio['id']; }

1


From what I understand, you want to filter this array by index id and name. To do this you can use the function array_filter of PHP.

Example:

Filtering this array by id A220 and By name Dipirona

<?php 

    $arr = "..."; //Array que você mandou no exemplo acima

    $novoArray = array_filter($arr, function($item) {
        return $item['id'] == "A220" && $item['name'] == "Dipirona"; 
    });

Upshot:

Array
(
    [0] => Array
        (
            [id] => A220
            [name] => Dipirona
            [symbol] => R$
            [rank] => 1
            [price_br] => 8.4
            [price_usd] => 2.0
            [total_supply] => 287.0
            [max_supply] => 21000.0
            [last_updated] => 1519152868
        )

)

If you want to get the first index ($novoArray[0]), use the command current:

$novoArray = current($novoArray);

Then the result will be this:

Array
(
    [id] => A220
    [name] => Dipirona
    [symbol] => R$
    [rank] => 1
    [price_br] => 8.4
    [price_usd] => 2.0
    [total_supply] => 287.0
    [max_supply] => 21000.0
    [last_updated] => 1519152868
)
  • Gabriel, can you help me? You have this link https://api.coinmarketcap.com/v1/ticker/ and I would like to take this array and import it into a local array as it does?

  • 1

    You can use the command file_get_contents('https://api.coinmarketcap.com/v1/ticker/') to pull the data and command json_decode() to convert from json to array. The line would look like this: $dados = json_decode(file_get_contents('https://api.coinmarketcap.com/v1/ticker/'))

  • Related to your reply, I made the code adapted with the URL but returns nothing.

  • Look if you haven’t forgotten the semicolon at the end (;). I didn’t put it in the line above.

  • I tried to run this script now here on my maqui, it worked properly. $dados = json_decode(file_get_contents('https://api.coinmarketcap.com/v1/ticker/')); The variable $dados contains the array with the items of this API.

Browser other questions tagged

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