PHP beginner here. I want to display compared Data ($myCoins) from a JSON API, so that only those Items will be displayed that I first inserted into the $myCoins arrays, but I can't figure out how to get the code to operate the same with NAMES instead of NUMBERS.
Used API:
https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,MIOTA,XRP,XLM,TRX&tsyms=USD
CODE:
<?php
//// API
$coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,IOTA,XRP,XLM,TRX,LINK&tsyms=USD'), true);
// Arrays
$myCoins = array(
'BTC' => array ( 'balance' => 12.90 ),
'ETH' => array ( 'balance' => 122.23 ),
'XRB' => array ( 'balance' => 221.52 ),
'MIOTA' => array ('balance' => 233.00 ),
'XRP' => array ( 'balance' => 429.00 ),
'XLM' => array ( 'balance' => 1205.89 ),
'TRX' => array ( 'balance' => 5299.40 )
);
// Fetch the Coins
$numCoins = sizeof ($coinData['RAW']);
$portfolioValue = 0;
for ( $xx=0; $xx<$numCoins; $xx++) {
// Comparing to Data
$thisCoinSymbol = $coinData[$xx]['FROMSYMBOL'];
//
$coinHeld = array_key_exists($thisCoinSymbol, $myCoins);
// Only retour held
if ( !$coinHeld ) { continue; }
// names test:
echo $coinData[$xx]['FROMSYMBOL'];
};
?>
The Tutorial I used was made with an API that displays the Items as numbers, not the Item names like in the API I want to use.
Tutorial API:
https://api.coinmarketcap.com/v1/ticker/
So my console puts out a Undefined offset: 0
- Undefined offset: 7
on line $thisCoinSymbol = $coinData[$xx]['FROMSYMBOL']
; I understand that this is because $xx equals 0 - 7, because there are 8 Items under ['RAW'], and there is no 0-7 in the API I use.
How would i get the same result as the Tutorial but with the API I want to use?
You should use a foreach to parse your json that is structured like that:
RAW: {
BTC: {
USD: {
TYPE: "5",
MARKET: "CCCAGG",
FROMSYMBOL: "BTC",
....
}
},
ETH: {....},
...
}
So your code should looks like that:
<?php
$coinData = json_decode(file_get_contents('https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,XRB,IOTA,XRP,XLM,TRX,LINK&tsyms=USD'), true);
$myCoins = array(
'BTC' => array ( 'balance' => 12.90 ),
'ETH' => array ( 'balance' => 122.23 ),
'XRB' => array ( 'balance' => 221.52 ),
'MIOTA' => array ('balance' => 233.00 ),
'XRP' => array ( 'balance' => 429.00 ),
'XLM' => array ( 'balance' => 1205.89 ),
'TRX' => array ( 'balance' => 5299.40 )
);
$portfolioValue = 0;
// your information in json path ['RAW'] so safeguard here to be sure it exists
if (isset($coinData['RAW'])) {
// then loop on all entries $cryptoSymbol will contains for example BTC and cryptoInfo the array USD => [...]
foreach($coinData['RAW'] as $cryptoSymbol => $cryptoInfo) {
// safeguard, check path [USD][FROMSYMBOL] exists
if (!isset($cryptoInfo['USD']) || !isset($cryptoInfo['USD']['FROMSYMBOL'])) {
// log or do whatever to handle error here
echo "no path [USD][FROMSYMBOL] found for crypto: " . $cryptoSymbol . PHP_EOL;
continue;
}
// Your symbol in on your json path/array [USD][FROMSYMBOL]
$thisCoinSymbol = $cryptoInfo['USD']['FROMSYMBOL'];
$coinHeld = array_key_exists($thisCoinSymbol, $myCoins);
// Only retour held
if ( !$coinHeld ) { continue; }
echo $cryptoInfo['USD']['FROMSYMBOL'] . PHP_EOL;
}
}
?>
You will have the output:
BTC ETH XRB XRP XLM TRX
Now you can parse the json in the right way, it should be easy for you to build the expected json.
Have a look at the foreach doc here: https://www.php.net/manual/en/control-structures.foreach.php