我有一个多维数组,打印出我想要的方式,但是,我已经不知道如何将它构建到我正在寻找的每个循环中。
请注意:$ 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>
另外,如果我让自己变得复杂,请告诉我。
答案 0 :(得分: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>
答案 1 :(得分:1)
如果我理解正确的话,我认为这就是你要找的。 p>
<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>
答案 2 :(得分: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; ?>
答案 3 :(得分: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;
?>
答案 4 :(得分: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
是与该键相关联的数组。
答案 5 :(得分: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>