0

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

3 Answers 3

1

Your counter variable isn't doing anything as your foreach will only iterate over $inner[0] and then stop. You need to add a third level of iteration:

foreach($pixa_feedback as $inner){
    // check type
    if(is_array($inner)){
        // iterate through nested array
        foreach ($inner as $values){
            foreach ($values as $key => $value) {
                echo $key . ": " . $value . " <br>";
            }
        }
    }
}

Output:

url: www.someurl.com <br>id: 11 <br>
url: www.differenturl.com <br>id: 22 <br>

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much - this works! This was the most specific answer thus I will flag this as the correct answer!
0

You weren't looping through your array correctly Try this:

$temp = array (
   'totalHits' => 500,
   'total'     => 845,
   'hits' =>
   array (
       0 => array (
              'url' => 'www.someurl.com',
              'id'  => '11',
       ),
       1 => array (
              'url' => 'www.differenturl.com',
              'id'  => '22',
       ),
 ));

foreach($temp["hits"] as $inner_arr){
     echo $inner_arr["url"];
     echo $inner_arr["id"];
}

Comments

0

In a foreach you do not need a counter variable. The loop-body will run for every item in the first "argument". Thus the inner loop (during the outer-loop's 3rd iteration) iterates over array ('url' => 'www.someurl.com', 'id' => '11') and finishes afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.