I have the below JSON I need to loop through in PHP.
{
"page": 1,
"perPage": 3,
"count": 607,
"status": "OK",
"tickers": [
{
"ticker": "A",
"name": "Agilent Technologies Inc.",
"market": "STOCKS",
"locale": "US",
"type": "CS",
"currency": "USD",
"active": true,
"primaryExch": "NYE",
"updated": "2020-12-15",
"codes": {
"cik": "0001090872",
"figiuid": "EQ0087231700001000",
"scfigi": "BBG001SCTQY4",
"cfigi": "BBG000C2V3D6",
"figi": "BBG000C2V3D6"
},
"url": ""
},
{
"ticker": "AA",
"name": "Alcoa Corporation",
"market": "STOCKS",
"locale": "US",
"type": "CS",
"currency": "USD",
"active": true,
"primaryExch": "NYE",
"updated": "2020-12-15",
"codes": {
"cik": "0001675149",
"figiuid": "EQ0000000045469815",
"scfigi": "BBG00B3T3HF1",
"cfigi": "BBG00B3T3HD3",
"figi": "BBG00B3T3HD3"
},
"url": ""
},
{
"ticker": "WADV",
"name": "Wireless Advantage Inc Common Stock",
"market": "STOCKS",
"locale": "US",
"type": "CS",
"currency": "USD",
"active": true,
"primaryExch": "GREY",
"updated": "2020-03-30",
"codes": {
"figiuid": "EQ0010295500001000",
"scfigi": "BBG001S87270",
"cfigi": "BBG000DKG4K2",
"figi": "BBG000DKG4K2"
},
"url": ""
}
]
}
This is my function to loop through - I simply only need the "ticker" from each of the nested arrays.
public function getTickers()
{
$APIKey = "API_KEY";
$tickers_request = 'API_URL';
$session = curl_init($tickers_request);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$json = substr($response, strpos($response, "{"));
$result = json_decode($json, true);
$ticker =[];
foreach($result as $results)
{
foreach ($results['tickers'] as $ticker)
{
$ticker['ticker'] = $results['ticker'];
}
}
return $ticker;
}
I receive these two errors in PHP -
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Severity: Notice
Message: Array to string conversion
What's wrong with my function? I'm a seasoned PHP developer but perhaps I am missing something...