F
F
FRATEREH2022-03-30 16:17:12
PHP
FRATEREH, 2022-03-30 16:17:12

How to display the entire value of the array and not just one?

I have a primary array

{
  "results": [
    {
      "question": "Ты кто?",
      "answer": "Вася",
      "topic": [
        "Имя"
      ]
    },
  {
      "question": "Сколько лет?",
      "answer": "5",
      "topic": [
        "Возраст"
      ]
    }
}


The task is to display the Name first, then the Question and Answer.
But if I write a nested foreach, then it will first display all the data of the Question and Answer array in a loop, and only then the Name, it also happens if each element is displayed separately, and then concatenate like $q.$q2

So I wrote this code

$qan1 = []; $qan2 = []; $qan3 = []; $qan5 = '';
foreach ($data['results'] as $repository2){ foreach ($repository2['topic'] as $repository3){
$qan3[] = $repository3;}
$qan1[] = $repository2['question'];
$qan2[] = $repository2['answer'];}
$qan4 = array($qan3,$qan1,$qan2);
foreach ($qan4 as $repos):
$qan5 .= '<dt><span>'.$repos[0].'</span><div class="acc-icon-wrap parallax-wrap"><div class="acc-button-icon parallax-element"><i class="fa fa-angle-down"></i></div></div></dt><dd class="accordion-content"><h3>Question</h3><dd>'.$repos[1].'</dd><h3>Answer</h3><dd>'.$repos[2].'</dd></div>';
endforeach;
return $qan5;


In which I display all the elements in arrays and make them a common array

in $qan4, I get something like this

(
    [0] => Array
        (
            [0] => Имя
            [1] => Возраст
         )
[1] => Array
        (
            [0] => Ты кто?
            [1] => Сколько лет?
         )
[2] => Array
        (
            [0] => Вася
            [1] => 5
         )
)


But when I do this part of the code the problem starts

foreach ($qan4 as $repos):
$qan5 .= '<dt><span>'.$repos[0].'</span><div class="acc-icon-wrap parallax-wrap"><div class="acc-button-icon parallax-element"><i class="fa fa-angle-down"></i></div></div></dt><dd class="accordion-content"><h3>Question</h3><dd>'.$repos[1].'</dd><h3>Answer</h3><dd>'.$repos[2].'</dd></div>';
endforeach;
return $qan5;


Because instead of displaying all array values ​​in $repos[0] it displays only Vasya, and then $repos[1] How many years is generally incorrect and I don't understand how to fix it,

because if I write like this

($qan4[0] as $reposit) : echo $reposit; endforeach;


then outputs all elements of the key correctly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2022-03-30
@FRATEREH

So, first of all, you don't need to output via echo html.
Secondly, you have all the necessary keys in the cycle, you seem to be dumb, so I explain it on my fingers, I hope it helps ...

<?php
...
foreach ($data['results'] as $repo){ 
?> //закрыли пхп, дальше выводим хтмл 
<dt>
   <span>
         <?=join(', ', $repo['topic']);?> //сделали пхп вывод где надо, и дальше выводим хтмл... 
   </span> 
   <div class="acc-icon-wrap parallax-wrap">
      <div class="acc-button-icon parallax-element">
          <i class="fa fa-angle-down"></i>
       </div>
   </div>
</dt>
<dd class="accordion-content">
   <h3>Question</h3>
<dd><?=$repo['question']?></dd>
<h3>Answer</h3>
<dd><?=$repo['ansver']?></dd>
<?php } ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question