【问题标题】:PHP: How to use two dimensional array to display menu?PHP:如何使用二维数组显示菜单?
【发布时间】:2015-05-05 20:16:13
【问题描述】:

我认为我做得很好,但我只是坚持这一点。我想使用数组和 foreach 循环来显示这个菜单:

<a href="/design/payments"><img src="/img/page-icons/credit-card21.png" />Payments</a>
<a target="_blank" href="/cloud"><img src="/img/page-icons/upload123.png" />Cloud</a>
<a target="_blank" href="/portal"><img src="/img/page-icons/earth208.png" />Portal</a>

为此,我需要在 PHP 中将其转换为这一行:

echo '<a href="' . $link . '" target="' . $target . '"><img src="/img/page-icons/' . $image . '" />' . $title . '</a>';

要在循环中填充它,我们需要创建类似这样的东西......这就是我卡住的地方:

foreach( $stamp as $link => $target ){
    echo '<a href="/' . $link . '" target="' . $target . '">';

    foreach( $stamp[] as $title => $image ){
        echo '<img src="/img/page-icons/' . $image . '" />' . $title;
    }

    echo '</a>';
}

我真的不知道如何处理上述问题,今天只是在搞砸了一段时间。我也不想总是在每个链接上显示target="' . $target . '"

数组可能是一个二维数组?无论如何都是这样的:

$stamp = array(
    'link' => array('title' => 'image'),
    'link' => array('title' => 'image'),
    'link' => array('title' => 'image')
);

编辑:

“目标”是什么有些混淆,我想将数组中的 4 个值回显到链接中,目标是其中一个值。我不知道如何在数组中使用它,所以我把它省略了。

【问题讨论】:

  • 这里的目标是什么?

标签: php arrays multidimensional-array


【解决方案1】:

当你这样做时:

foreach( $stamp as $link => $target )

$link 变量包含字符串“link”,$target 变量是一个数组,例如 ['title' =&gt; 'image']

你应该做的是有一个这样的数组:

// Init links
$links = array();

// Add links
$links[] = array(
   'title' => 'My Title',
   'href' => 'http://www.google.com',
   'target' => '_blank',
   'image' => 'image.png',
);

foreach ($links as $link)
{
   echo '<a href="'.$link['href'].'" target="'.$link['target'].'">';
       echo '<img src="/img/page-icons/' . $link['image'] . '" />';
       echo $link['title']; 
   echo '</a>';
}

这是一种更灵活的方法,可让您在未来将其他数据项添加到结构中。如果您还有不同的数据源(例如数据库),则可以在循环中轻松生成 $links 数组。


编辑

要回答您的进一步问题,您可以在链接构建前加上一组合理的默认值,如下所示:

foreach ($links as $link)
{
   // Use the ternary operator to specify a default if empty
   $href = empty($link['href']) ? '#' : $link['href'];
   $target = empty($link['target']) ? '_self' : $link['target'];
   $image = empty($link['image']) ? 'no-icon.png' : $link['image'];
   $title = empty($link['title']) ? 'Untitled' : $link['title'];

   // Write link
   echo "<a href='$href' target='$target'>";
       echo "<img src='/img/page-icons/$image' />";
       echo $title; 
   echo "</a>";
}

【讨论】:

  • 谢谢!现在有了您提供的内容,我将如何查看数组中是否存在目标,如果存在则回显目标,如果不存在则回显没有target="" 的链接?在我看来,我看到它是通过一个 if 语句完成的,比如 if (array_key_exists('target', $links)) {}?
  • 我用一些额外的帮助更新了我的答案。如果我的回答对你有帮助,你能接受吗?
  • 有没有办法检查目标是否真的在数组中?
【解决方案2】:

你可以像这样设置你的数组:

$stamp = array(
 '0' => array('title'=>$title 'image'=>$image,'link'=>$link,'target'=>$target),
 '1' => array('title'=>$title, 'image'=>$image,'link'=>$link,,'target'=>$target),
 '2' => array('title'=>$title 'image'=>$image,'link'=>$link,'target'=>$target)
);

在foreach中你可以写

$i=0;
foreach( $stamp as $st=> $target ){
echo '<a href="/' . $st['link'] . '" target="' . $st['target'] . '">';


    echo '<img src="/img/page-icons/' . $st['image'] . '" />' . $st['title'];


echo '</a>';

}

【讨论】:

    猜你喜欢
    • 2015-07-05
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多