0

我有一个多维数组,可以准确地打印出我想要的样子,但是,我一直在弄清楚如何将它构建到我正在寻找的 for each 循环中。

请注意:$handle 是客户端在后端输入的字段。

<?php
$gp = Mage::getStoreConfig('social_code/social_group/google_field');
$ld = Mage::getStoreConfig('social_code/social_group/linkedin_field');
$tw = Mage::getStoreConfig('social_code/social_group/twitter_field');
$fb = Mage::getStoreConfig('social_code/social_group/facebook_field');

$social_array = array(
    "facebook" => array(
        'class' => "facebook",
        'url' => 'https://www.facebook.com/',
        'handle' => $fb
    ),
    "twitter" => array(
        'class' => "twitter",
        'url' => 'https://www.twitter.com/',
        'handle' => $tw
    ),
    "linked-in" => array(
        'class' => "linked-in",
        'url' => 'http://www.linkedin.com/company/',
        'handle' => $ld
    ),
    "google-plus" => array(
        'class' => "google-plus",
        'url' => 'https://plus.google.com/',
        'handle' => $gp
    )
);
?>

现在我希望将其作为无序列表吐出,所以我有以下内容,但它仍然不适合我。

<ul>
        <?php foreach ($social_array as $name => $group) :?>
            <?php foreach ($group as $class => $url) :?>
                <li><a href="<?php echo ("$url"); ?>" class="<?php echo ("$class;") ?>"><?php echo ("$group"); ?></a></li>
            <?php endforeach; ?>
        <?php endforeach; ?>
    </ul>

我想要它,以便它遍历所有社交数组并打印与此类似的东西

<li><a href="<?php echo ($url);?><?php echo ($handle);?>" class="<?php echo ($class); ?>"><?php echo $name; ?></a></li>

或者更好理解

<li><a href="http://www.facebook.com/handle" class="facebook">Facebook</a></li>

另外,如果我让自己变得复杂,请告诉我。

4

6 回答 6

1
<ul>
    <?php foreach ($social_array as $name => $group) :?>
        <li><a href="<?php echo $group['url'].$group['handle']; ?>" class="<?php echo $group['class']; ?>"><?php echo $name; ?></a></li>
    <?php endforeach; ?>
</ul>
于 2013-08-28T14:45:10.543 回答
1

如果我理解正确,我认为这就是您要寻找的东西。

<ul>
        <?php foreach ($social_array as $name => $group) :?>
                <li><a href="<?php echo $group['url'].$group[handle']; ?>" class="<?php echo $group['class']; ?>"><?php echo $name
        <?php endforeach; ?>
</ul>
于 2013-08-28T14:46:02.567 回答
1

无需内部 foreach。

<?php foreach ($social_array as $name => $group) :?>
        <li><a href="<?php echo $group['url'] ?>" class="<?php echo $group['class'] ?>"><?php echo $group['class']; ?></a></li>
<?php endforeach; ?>
于 2013-08-28T14:48:45.033 回答
1

请参阅http://3v4l.org/3cH6f了解下面的示例。只有一个foreach。

<?php
$social_array = array(
    "facebook" => array(
        'class' => "facebook",
        'url' => 'https://www.facebook.com/',
        'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
    ),
    "twitter" => array(
        'class' => "facebook",
        'url' => 'https://www.facebook.com/',
        'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
    ),
    "linked-in" => array(
        'class' => "facebook",
        'url' => 'https://www.facebook.com/',
        'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
    ),
    "google-plus" => array(
        'class' => "facebook",
        'url' => 'https://www.facebook.com/',
        'handle' => 'Mage::getStoreConfig(\'social_code/social_group/twitter_field\')'
    )
);

$html = '<ul>';

foreach ($social_array as $name => $class_array){
        $html .= '<li><a href="' . $class_array['url'] . $class_array['handle']  .'" class="' . $class_array['class']. '">'. $name. '</a></li>';
}
$html .= '</ul>';

print $html;
?>
于 2013-08-28T14:53:05.273 回答
0

试一试:

<ul>
    <?php foreach ($social_array as $name => $properties) :?>
        <li><a href="<?php echo ($properties["url"] . $properties["handle"]); ?>" class="<?php echo ($properties["class"]); ?>"><?php echo ucfirst($name); ?></a></li>
    <?php endforeach; ?>
</ul>

在这个 foreach 中,语法是foreach($array as $key => $value),所以这里$name是来自 $social_array 的键,并且$properties是与该键关联的数组。

于 2013-08-28T14:44:12.380 回答
0

您忘记的是内部foreach正在处理每个项目,即class,ulr,handle一次处理一个项目。

因此,您需要在传递 3 个项目时存储所需的信息,以便在您拥有实际需要的 2 个项目后使用它。

所以这可能会有所帮助。

<ul>
<?php 
    foreach ($social_array as $name => $group) :

        $class    = NULL;
        $url      = NULL;
        $handle   = NULL;

        foreach ($group as $name => $value) :

            switch ($name) :
               case 'class'   : $class  = $value;     break;
               case 'url'     : $url    = $value;     break;
               case 'handle'  : $handle = $value;     break;
            endswitch;

            if ( isset($class) AND isset($url) AND isset($handle) ) :

               echo '<li><a href="' . $url.$handle . '" class="' . $class . '">' . $group . '</a></li>';

               $class   = NULL;
               $url     = NULL;
               $handle  = NULL;
            endif;

        endforeach;

    endforeach;
?>
</ul>
于 2013-08-28T14:55:48.030 回答