Aron
Aron

Reputation: 135

foreach with array_chunk in php and with multiple arrays

I'm relatively new to php and I've been trying all day long to get this to work. I have a multiple array and want to echo each one in a specific format, and in groups. So i've gone through stackoverflow and found this help:

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>


        <li style='display:block'>


                     <div class="film"> <?php echo $curta[0]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[1]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[2]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[3]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[4]['titulo']; ?></div>
                     <div class="film"> <?php echo $curta[5]['titulo']; ?></div>


             </li> 

      <? }; ?>

And this returns what I want but the last items of the array doesnt fill up to 6 and creates 2 extras empty divs and messes up the design.

This is a single example of the array i have:

<?php
 $projetos = array ( 

            "ugm" => array (
                    "id" => "ugm",
                    "titulo" => "Una Guerra Más",
                    "video" => "imagem",
                    "videoid" => "",
                    "height" => "$video_height_wide",
                    "sinopse" => "Um soldado moribundo deseja enviar sua última carta. Curta indisponível por exibição em festivais. Feito em parceria com a Universidad del Cine e LightBox Studios.",
                    "elenco" => "Ignacio J. Durruty - Rodrigo Soler - Ulisses Levanavicius - Aron Matschulat Aguiar",
                    "idioma" => "Inglês - Português",
                    "camera" => "Sony EX1",
                    "formato" => "HD",
                    "duracao" => "9'55''",
                    "ano" => "2012",
                    "tipo" => "Curta",
                    "credito" => "Direção - Edição - Produção - Roteiro",
                                 ), (...)

I want to be able to edit just one div that will be the master for the others... and using the implode I've read on another question but didn't worked to echo the strings I wanted..

Would please someone help out? thanks in advance!

Upvotes: 11

Views: 30790

Answers (4)

moonwave99
moonwave99

Reputation: 22797

but the last items of the array doesnt fill up to 6

As this is going to happen most of the time, you can't assume that every chunk has 6 elements, so you'll have to iterate over the chunk:

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>

<li style='display:block'>

<? foreach($curta as $c) { ?>       
    <div class="film"> <?php echo $c['ugm']['titulo']; ?></div>
<? }; ?>

</li> 

<? }; ?>

This way you are sure to display no empty divs.

Upvotes: 0

knittl
knittl

Reputation: 265533

Why not use a loop to iterate over $curta?

<? foreach(array_chunk($projetos, 6) as $curta ) { ?>
  <li style='display:block'>
    <? foreach($curta as $c) { ?>
      <div class="film"><? echo $c['titulo']; ?></div>
    <? } ?>
  </li> 
<? }; ?>

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65294

<?php foreach(array_chunk($projetos, 6) as $curta ) { ?>
    <li style='display:block'>
       <?php foreach($curta as $detail) { ?>
                 <div class="film"> <?php echo $detail['titulo']; ?></div>
       <?php } ?>
    </li> 
<? }; ?>

Upvotes: 28

This lines:

<div class="film"> <?php echo $curta[0]['titulo']; ?></div>

should be like this:

<div class="film"> <?php echo $curta[0]['ugm']['titulo']; ?></div>

that should do what you want.

Upvotes: -1

Related Questions