foreach with array_chunk in php and with multiple arrays
Asked Answered
B

4

11

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!

Baun answered 16/9, 2012 at 15:33 Comment(1)
sorry friends, but i cant understand your problem...it's my mistake..if you elaborate then i will help you as possible.Opia
C
28
<?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> 
<? }; ?>
Cassilda answered 16/9, 2012 at 15:39 Comment(1)
thanks eugen! this worked perfectly! ;) knittl did the same thing and im using this code now u guys provided :) thanks a lot!Baun
C
0

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> 
<? }; ?>
Cognition answered 16/9, 2012 at 15:41 Comment(0)
F
0

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.

Filomena answered 16/9, 2012 at 15:42 Comment(0)
P
-1

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.

Pitiless answered 16/9, 2012 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.