2013-08-28 207 views
0

我有一個多維數組,它完全打印出我想要的數據,但是,我已經陷入了搞砸了如何將它構造到每個循環中尋找。請注意0​​:$ handle是客戶端在後端輸入的字段。多維數組 - foreach在一個foreach內

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

而且,如果我做這對於我自己來說很複雜,請讓我知道。

回答

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> 
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是與該鍵關聯數組。

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

這看起來像它可以派上用場,我會試試看。謝謝。 – iamgraeme