0

我希望抵消一组 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
}?>

PS 对我如何格式化上述代码块中的代码表示歉意——我仍然不能 100% 确定如何让它在 StackOverflow 上看起来“不错”。

4

3 回答 3

2

用于array_chunk将一维数组分解为二维数组。然后你可以遍历每个块,然后是每个结果,以获得它们之间的“偏移”效果。

$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
}
于 2013-02-24T12:29:20.667 回答
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']))
{
于 2013-02-24T12:34:08.223 回答
0

我不完全确定您在寻找什么,但这是我将如何格式化您的代码。'if' 的替代语法更具可读性,并且使用for 循环而不是 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; ?>
于 2013-02-24T12:48:52.787 回答