PHP массив не работает правильно

0

У меня возникают проблемы с тем, чтобы мой массив работал правильно. Я вытаскиваю параметры из своего шаблона Joomla, а затем использую их в основном файле индекса: вот что у меня есть:

Это выполняется во включенном файле, который я извлекаю первым

//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>

Может кто-нибудь, пожалуйста, скажите мне, что я делаю неправильно?

Теги:
multidimensional-array
joomla
joomla3.0

1 ответ

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");
};

Ещё вопросы

Сообщество Overcoder
Наверх
Меню