PHP数组无法正常工作

时间:2014-11-11 14:17:21

标签: php joomla multidimensional-array joomla3.0

我在让阵列正常工作时遇到了一些麻烦。我从我的Joomla模板中拉出params,然后在主索引文件中使用它们:这是我到目前为止所拥有的:

这是在我首先提取的包含文件中保存的

//Social Icons
$tbFacebook = $this->params->get('tbFacebook');
$tbTwitter = $this->params->get('tbTwitter');
$tbGoogle = $this->params->get('tbGoogle');
$tbLinkedIn = $this->params->get('tbLinkedIn');
$tbPinterest = $this->params->get('tbPinterest');
$tbYouTube = $this->params->get('tbYouTube');
$tbVimeo = $this->params->get('tbVimeo');

//Check all the text boxes for content and assign array information
//This may be able to be handled differently but for simplicity I left it alone 

if ($tbFacebook != null) { 
$arrFacebook= array("href" => $tbFacebook, "title" => "Like Us On Facebook", "icon" => "<i class=\"fa fa-facebook".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbTwitter != null) {
$arrTwitter = array("href" => $tbTwitter , "title" => "Follow Us On Twitter", "icon" => "<i class=\"fa fa-twitter".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbGoogle != null) {
$arrGoogle = array("href" => $tbGoogle , "title" => "Follow Us On Google Plus", "icon" => "<i class=\"fa fa-google-plus".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbLinkedIn != null) {
$arrLinkedIn = array("href" => $tbLinkedIn , "title" => "Connect With Us On LinkedIn", "icon" => "<i class=\"fa fa-linkedin".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbPinterest != null) {
$arrPinterest = array("href" => $tbPinterest , "title" => "Pin Us On Pinterest", "icon" => "<i class=\"fa fa-pinterest".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbYouTube != null) {
$arrYouTube = array("href" => $tbYouTube , "title" => "Watch Us On YouTube", "icon" => "<i class=\"fa fa-youtube".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbVimeo != null) {
$arrVimeo = array("href" => $tbVimeo , "title" => "Watch Us On Vimeo", "icon" => "<i class=\"fa fa-vimeo-square fa-2x\"></i>", "target" => "blank");
};

//Create Social Array
$arrSocial = array(
    $arrFacebook,
    $arrTwitter,
    $arrGoogle,
    $arrLinkedIn,
    $arrPinterest,
    $arrYouTube,
    $arrVimeo           
);

//Remove the empties
foreach($arrSocial as $key => $value) {
    if (empty($value)) {
        unset($arrSocial[$key]);
        $arrSocial = array_values($arrSocial);
    };
}

//Variable to determine fist item, incase its not phone
$first = true;

我的Joomla代码

<?php if ($this->countModules('footer_mobile')) : ?>
                <jdoc:include type="modules" name="footer_mobile" style="html5" />
                <?php else : 
                //Limit to the first 5 items
                for($i=0;$i<5;$i++){
                    if ( $first ) { 
                    $first = false;
                        echo '<div class="col-xs-2 col-xs-offset-1"><a href="'.$arrMobileFooter[$i]["href"].'" title="'.$arrMobileFooter[$i]["title"].'" target="_'.$arrMobileFooter[$i]["target"].'" ">'.$arrMobileFooter[$i]["icon"].'</a></div>';
                    } else { 
                    echo '<div class="col-xs-2"><a href="'.$arrMobileFooter[$i]["href"].'" title="'.$arrMobileFooter[$i]["title"].'" target="_'.$arrMobileFooter[$i]["target"].'">'.$arrMobileFooter[$i]["icon"].'</a></div>';
                    };
                };
                endif; ?>

当文件输出时它只会拉入Facebook链接,即使for循环正在创建其余部分而不是填充它:

<div class="col-xs-2 col-xs-offset-1">
    <a href="https://www.facebook.com/pages/United-Methodist-Camp-Tekoa-Inc/304907509358" title="Like Us On Facebook" target="_blank">
        <i class="fa fa-facebook-square fa-2x"></i>
    </a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_" "=""></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:1)

我猜想问题是当你试图在$arrSocial内操纵foreach时。我建议您重写此内容,只有在null使用$arrSocial[] = value添加到现有数组时才将对象添加到数组中。

试试这个:

//Social Icons
$tbFacebook = $this->params->get('tbFacebook');
$tbTwitter = $this->params->get('tbTwitter');
$tbGoogle = $this->params->get('tbGoogle');
$tbLinkedIn = $this->params->get('tbLinkedIn');
$tbPinterest = $this->params->get('tbPinterest');
$tbYouTube = $this->params->get('tbYouTube');
$tbVimeo = $this->params->get('tbVimeo');

$arrSocial = array();

if ($tbFacebook != null) { 
    $arrSocial[] = array("href" => $tbFacebook, "title" => "Like Us On Facebook", "icon" => "<i class=\"fa fa-facebook".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbTwitter != null) {
    $arrSocial[]  = array("href" => $tbTwitter , "title" => "Follow Us On Twitter", "icon" => "<i class=\"fa fa-twitter".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbGoogle != null) {
    $arrSocial[] = array("href" => $tbGoogle , "title" => "Follow Us On Google Plus", "icon" => "<i class=\"fa fa-google-plus".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbLinkedIn != null) {
    $arrSocial[] = array("href" => $tbLinkedIn , "title" => "Connect With Us On LinkedIn", "icon" => "<i class=\"fa fa-linkedin".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbPinterest != null) {
    $arrPinterest = array("href" => $tbPinterest , "title" => "Pin Us On Pinterest", "icon" => "<i class=\"fa fa-pinterest".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbYouTube != null) {
    $arrSocial[] = array("href" => $tbYouTube , "title" => "Watch Us On YouTube", "icon" => "<i class=\"fa fa-youtube".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbVimeo != null) {
    $arrSocial[] = array("href" => $tbVimeo , "title" => "Watch Us On Vimeo", "icon" => "<i class=\"fa fa-vimeo-square fa-2x\"></i>", "target" => "blank");
};