多维数组-foreach,在foreach中


Multidimensional Array - foreach, within a foreach

我有一个多维数组,它正是我想要的打印方式,然而,我一直在想如何将它构建到我想要的每个循环中。

请注意:$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>

如果我把这件事搞得太复杂了,请告诉我。

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

如果我理解正确,我想这就是你想要的。

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

不需要内部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; ?>

请参阅http://3v4l.org/3cH6f对于下面工作的示例。只有一个前臂。

<?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;
?>

尝试一下:

<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是与该键关联的数组。

您忘记的是内部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>