在生成数组的PHP for循环中动态插入添加的逻辑

在生成数组的PHP for循环中动态插入添加的逻辑,php,for-loop,logic,Php,For Loop,Logic,情况: 我有一个首页滑块,用户可以从选项菜单中选择要包含在首页滑块中的页面。选项存储在一个静态数组中,仅打印用户的选择,直到我确定的限制,请参阅下面的代码 <? // Counts the Number of Boxes available for the front page slider $numberOfBoxes = 6; $group = array (); // Create an empty array to store a

情况: 我有一个首页滑块,用户可以从选项菜单中选择要包含在首页滑块中的页面。选项存储在一个静态数组中,仅打印用户的选择,直到我确定的限制,请参阅下面的代码

<?
        // Counts the Number of Boxes available for the front page slider
        $numberOfBoxes = 6; 

        $group = array (); // Create an empty array to store all of your final data


        //Counts all the checkboxes and their corresponding sliderboxes
        for ( $a = 1; $a <= $numberOfBoxes; $a++ )
        {
            if (${'checkBox_'.$a} == TRUE){

            $tempDefaultArray = ${'sliderBox_'.$a};
            array_push($group , $tempDefaultArray); // Push the data to the $group array
            }
        }

        $arraySize = sizeof($group); // Find the size of the final array

        // Take the outcome from the above calculations and create slider
        for ( $i = 0; $i <= ($arraySize - 1); $i++ )
        {
            $image = $group[$i]['image'];
            $title = $group[$i]['title'];
            $tagline = $group[$i]['tagline'];
            $url = $group[$i]['url'];
            $vanityUrl = $group[$i]['vanityUrl'];

            print'
            <li '.$sliderDivider.'>
            <img src="'.$image.'" border="0"/>
            <h1>'.$title.'</h1>
            <p>'.$tagline.'</p>
            <a href="'.$url.'" '.$sliderLink.'/>'.$vanityUrl.'</a>
            ';}?>

问题: 这非常有效,但我想声明,如果某个特定的外部变量设置为和/或true,则滑块中的第三个框将填充数组中特定对象的数据。我想保持这个现有逻辑的功能,如果设置了这个变量,只需加入一些额外的代码来覆盖第三个框

例如: 我选择滑块选项为:第1条、第2条、博客1、图像1,然后在自定义的单独区域中,用户选择YouTube视频。默认情况下,当该变量设置为$youTube时,那么第三个选择(在此ex Blog 1中)将默认为youTube。另外请注意,此默认框与静态选项位于同一数组中


这是一件很难解释的事情,如果他们是任何忍者PHPrs,可以推荐一种有效的方法来处理这个问题,我将不胜感激。

嗯,如果我理解你的问题,如果在数组变量之外设置了某个条件,你想在循环中有一个覆盖吗

您可以在循环中放入if语句来检查外部变量:

if($external_var=="something") {
    $image = $static_array['image'];
    $title = $static_array['title'];
    $tagline = $static_array['tagline'];
    $url = $static_array['url'];
    $vanityUrl = $static_array['vanityUrl'];
} else {
    $image = $group[$i]['image'];
    $title = $group[$i]['title'];
    $tagline = $group[$i]['tagline'];
    $url = $group[$i]['url'];
    $vanityUrl = $group[$i]['vanityUrl'];
}

感谢您的输入,但我只想在第三次迭代中执行。不要覆盖第三个位置,只需将其推回第四个位置。很抱歉,这不是您想要的,请查看以下帖子: