[英]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>
不需要內在地進行學習。
<?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 。 只是一個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;
?>
試一下:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.