0

I combine 4 arrays into a single array named 'questions' . how to display the elements inside this array one by one?

the PHP code is given below

<?php 
  $questions = array_merge($gk,$english,$malayalam,$maths);
  print_r($questions[1]); 
?>

and the print shows as given below

stdClass Object
(
[question_id] => 18
[question] => chairman of isro
[category_id] => 2
[exam_id] => 0
[subcategory_id] => 0
[category_name] => 
[subcategory_name] => 
[created] => 0000-00-00 00:00:00.00000
[modified] => 0000-00-00 00:00:00.00000
[option_a] => hg
[option_b] => k sivan
[option_c] => hg
[option_d] => fd
[correct_answer] => k sivan
[explanation] => 
)

how to display these items

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • 1
    What do you mean by "these items"? As you can see in the output, `$questions[1]` is not an array, but an object of type `stdClass` – Nico Haase May 09 '18 at 07:36
  • yes how to display these objects of type stdClass ?. when i merge array it automatically come like this. – sarath chandran May 09 '18 at 07:45
  • Possible duplicate of [PHP - Accessing Multidimensional Array Values](https://stackoverflow.com/questions/17139453/php-accessing-multidimensional-array-values) – LF00 May 09 '18 at 07:49

3 Answers3

1

foreach - is probably the most usable thing for that.

foreach($questions as $question) {
    var_dump($question->question); // "chairman of isro" for the first one
}

As your questions array contain stdClass instances, you can easily display them.

Alejandro
  • 5,834
  • 1
  • 19
  • 36
  • actually in $questions contain many questions . the printed result shows one of them. i want to display these questions and options one by one when clicking a next button – sarath chandran May 09 '18 at 07:29
  • Why not use nested if statements. I would have given a code example if the full structure of the array is given – Ikelie Cyril May 09 '18 at 07:42
  • I don't understand what you mean, but I don't see why `if` statement could be needed here – Alejandro May 09 '18 at 07:45
  • Array ( [0] => stdClass Object ( [question_id] => 15 [question] => Capital of India [category_id] => 2 [exam_id] => 0 [subcategory_id] => 0 [created] => 0000-00-00 00:00:00.00000 [modified] => 0000-00-00 00:00:00.00000 [option_a] => Tvm [option_b] => Delhi [option_c] => Agra [option_d] => Lahore [correct_answer] => Delhi [explanation] => ) ) – sarath chandran May 09 '18 at 07:48
  • yeah, you have an array of instances of `stdClass`, so just loop over the array and display question via -> name, this would work well – Alejandro May 09 '18 at 07:50
0

Hope this will help you :

To iterate and print u need foreach loop like this

$questions = array_merge($gk,$english,$malayalam,$maths);
if (! empty($questions))
{
  foreach($questions as $question) {
     echo $question->question_id; /*output 18*/
     echo $question->question; /*output chairman of isro*/
     /*..... all others*/
  }
}

For single question you can do like this :

 $questions[0]->question_id; 
 $questions[0]->question; 

 $questions[1]->question_id; /*output 18*/
 $questions[1]->question; /*output chairman of isro*/

for more : http://php.net/manual/en/control-structures.foreach.php

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • this is fine for displaying all questions at a time. but for displaying $questions[1] it is not working. how to display objects inside class ? – sarath chandran May 09 '18 at 07:42
0
$questions= json_decode(json_encode($questions), true);

So first of all get your object into array format if you want to handle it as an array (personally i prefer to have it in array than in object format).

You said that your questions have many questions with nested arrays. The only way to access array elements is by using a loop (foreach,whole,for).

So now that your $questions are inside an array you can go like this:

foreach($questions as $row){
    echo $row['question_id'];
    echo $row['question'];
//carry on as you wish.

}

That way you will get access to all your questions and their nested arrays. Inside the foreach you can write your code to handle your data inside the loop, or move some fields to a new array that's up to you.

My answer is based also on your comment to a previous answer about what you want to achieve.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36