I have an object like below, with arrays nested in each other. I use foreach loops to loop through the first and second level of the arrays.
array (
'totalHits' => 500,
'total' => 845,
'hits' =>
array (
0 => array (
'url' => 'www.someurl.com',
'id' => '11',
),
1 => array (
'url' => 'www.differenturl.com',
'id' => '22',
),
);
I am trying to get keys and values from all the arrays nested in 'hits', but I only get the first one, hits[0]. What am I missing?
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$keywords = $_POST['pixa_keyword'];
$api_key = "my_hidden_api_key";
$url = "https://pixabay.com/api/?key=".$api_key."&safesearch=true&q=".$keywords;
$json = file_get_contents($url);
}
?>
<body>
<form action="#" method="post">
<input name="pixa_keyword" id="cms_pixaSearchKeywords2" type="text" value="">
<input name="submit_pixa_search" id="cms_pixaSearchBtn2" type="submit" value="search">
</form>
<div id="cms_displayPixaResults"></div>
<?php
$pixa_feedback=[];
if($_SERVER['REQUEST_METHOD']=="POST"){
$pixa_feedback = json_decode($json);
}
foreach($pixa_feedback as $inner){
// check type
if(is_array($inner)){
// iterate through nested array
$i = 0;
foreach ($inner[$i] as $key => $value){
echo $key . ": " . $value . " <br>";
$i++;
}
}
}
?>