Print PHP array data

2

I am trying to print the data I have in the array. It works, I can see the data. My problem is that I want to print these various data within the same div. As I have so prints only one value. And I do not put the div in the foreach or make another div.

foreach($array_resultados as $key =>$value){
                    //echo "<label><b>".$value['title']."</b></label><br>";
                }
                echo 
                    "<td class='cal_today'><b>

                        <div class =divtoday>
                            <br>".$value['title'] ."
                        </div>
                    </td>"; 
    
asked by anonymous 04.11.2014 / 11:19

2 answers

0

You can make the following code:

$titles = "";
foreach($array_resultados as $key =>$value){
  $titles .= "<label><b>".$value['title']."</b></label><br>";
}
echo  "<td class='cal_today'><b>
          <div class =divtoday>".$titles."</div>
      </td>"; 
    
04.11.2014 / 12:11
5

The most practical way is to store the HTML that the cycle is generating in a variable and after that make use of the variable where it is intended:

// iniciar variável a vazio
$htmlDoForEach = '';

// por cada entrada na matriz
foreach ($array_resultados as $key => $value) {

    // gerar HTML e adicionar à variável
    $htmlDoForEach.= '
    <div class="divtoday">
        <br>'.$value["title"].'
    </div>';
}

// usar o HTML
echo '<div id="minhaGrandeDiv">'.$htmlDoForEach.'</div>';

Note: In your question code there are some things wrong with markup of HTML and with } in PHP that seem to be closing < before you are actually making use of the values in the array.

04.11.2014 / 11:26