简体   繁体   中英

Multidimensional Array - foreach, within a foreach

I have a multi dimensional array that is printing out exactly how I want it, however, I have become stuck on figuring out how I can construct it into the for each loop that i'm looking for.

Please Note : $handle is a field the client entered in the backend.

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

Now i wish to spit this out as an unordered list so i have the below, but its still not working for me.

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

I would like it so that it loops through all the social array and prins something similar to this

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

or so understood better

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

Also if I'm making this over complicated for myself please let me know.

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

I think this is what you're looking for if I've understood correctly.

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

There is no need to inner 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; ?>

See http://3v4l.org/3cH6f for the example working below. Just one 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;
?>

Give this a shot:

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

In this foreach, the syntax is foreach($array as $key => $value) , so here $name is the key from $social_array, and $properties is the array associated with that key.

The thing you are forgetting is that the inner foreach is processing each item ie class,ulr,handle one at a time.

You therefore need to store the information you require as you pass over each of the 3 items so it can be used once you have the 2 items you actually need.

So this may help.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM