抵消PHP数组结果

时间:2013-02-24 12:23:04

标签: php arrays offset

我希望将一组PHP数组结果偏移以创建多行数据。

例如,第一行将包含前四个数组结果,第二行显示结果5-8,第三行包含结果9-12。

目前,我在一个列表中打印出所有12个,但是我想要更多的显示控制(即将结果排序到我可以独立于每个样式的列中),因此为什么我要抵消结果

我目前的PHP如下:

<?php    
if (empty($tmdb_cast['cast'])) {

} else {?>
    <section class="cast">
        <p class="title">Cast</p>
        <ul class="section_body"><?php
            foreach($tmdb_cast['cast'] as $key => $castMember){
                if ($key < 12) {?>
                    <li class="actor_instance clearfix"><?php
                        if ($castMember['profile_path'] != '') {?>
                            <img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
                        } else {?>
                            <img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
                        }?>
                    <p class="actor"><?php echo $castMember['character']; ?> - <a href="actor.php?id=<?php echo $castMember['id']; ?>"><?php echo $castMember['name']; ?></a></p>
                    </li><?php
                }
            }?>

            <div class="morecast pull-right"><a href="http://www.imdb.com/title/<?php echo $imdb_id; ?>/fullcredits" title="View full cast list on IMDb">[...view all cast]</a></div>
        </ul>
    </section><?php
}?>

P.S。对于如何在上面的块中格式化代码感到抱歉 - 我仍然不能100%确定如何使它在StackOverflow上看起来“漂亮”。

3 个答案:

答案 0 :(得分:2)

使用array_chunk将单个维度数组分解为2D数组。然后你可以循环遍历每个块,然后循环每个结果,以获得它们之间的“偏移”效果。

$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have 

foreach($chunks as $key => $chunk)
{
    foreach($chunk as $castMember) 
    {
         //display castMember here
    }
    // add code between groups of 4 cast members here
}

答案 1 :(得分:0)

首先:混合php和html这种方式是一个非常糟糕的习惯...有一天你将不得不维护你的代码,那一天你会在你的桌子上呕吐,因为你在一个文件中混合了不同的语言...... 话虽如此.. 并且没有创建新数组:

foreach($tmdb_cast['cast'] as $key => $castMember)
{
    if ($key < 12)
    {
        // your code goes here

        // if key is 3, or 7 the code below will close the listcontainer and open a new one...
        if( ($key+1)%4 == 0 AND $key <11)
            echo '</ul> <ul class="section_body">';
    }
}

同样替换它:

if (empty($tmdb_cast['cast']))
{
}
else {

用这个:

if (!empty($tmdb_cast['cast']))
{

答案 2 :(得分:0)

我不完全确定你在寻找什么,但这是我如何格式化你的代码。 Alternative syntax对于'if'更具可读性,使用for loops代替foreach将使您可以控制您所说的行号。

<?php   if( ! empty($tmdb_cast['cast'])): ?>
    <section class="cast">
        <p class="title">Cast</p>
        <ul class="section_body">
<?php
            for($i = 0; $i < 12; $i++):
            $castMember = $tmdb_cast['cast'][$i];
?>
            <li class="actor_instance clearfix">
<?php           if($castMember['profile_path'] != ''): ?>
                <img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php           else: ?>
                <img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php           endif; ?>
                <p class="actor"><?php echo $castMember['character']; ?> - <a href="actor.php?id=<?php echo $castMember['id']; ?>"><?php echo $castMember['name']; ?></a></p>
            </li>
<?php       endfor; ?>

            <div class="morecast pull-right"><a href="http://www.imdb.com/title/<?php echo $imdb_id; ?>/fullcredits" title="View full cast list on IMDb">[...view all cast]</a></div>
        </ul>
    </section>
<?php   endif; ?>