有没有一种简单的方法可以从1D数组构建链接列表?
例如:
$array = [
'slug-1' => 'Title 1',
'slug-2' => 'Title 2',
'slug-3' => 'Title 3'
]
回响为:<a href="slug-1">Title 1</a>, <a href="slug-2">Title 2</a>, <a href="slug-3">Title 3</a>
基本上是一个改进的内爆函数,可以添加额外的参数作为class="something"
和类似的参数。
我怀疑是否存在,所以这就是我想要做的
创建全局函数:
- 创建
app/Http/helpers.php
- 通过在
autoload
块中添加"files": ["app/Http/helpers.php"]
来编辑composer.json
- 运行
composer dump-autoload
如本文所述。
不知道如何从这里开始。我尝试将以下代码添加到helpers.php
:
<?php
/**
* Given the array of type ['slug' => 'title', ...]
* create new array of type [ '0' => '<a href="slug">title</a>', ...]
* if $attributes given (also array) as ['id'=>'newLink', 'class'=>'newClass']
* add them to first array to get <a href='slug' id='newLink' class='newClass'>title</a>
*
*
* @param $array, $attributes
* @return array
*/
public function linkifyArray($array, $attributes){
$arrayOfLinks = $array;
return $arrayOfLinks;
}
这没有任何作用,但我试图在评论中解释我想做什么。
我相信我可以自己创建这个函数,但当我将上面的代码复制到helpers.php
文件时,我遇到了内部服务器错误,PHPStorm告诉我他是expecting statement
。
因此,我在这里缺少了一些非常基本的东西,在我可以继续创建我的助手函数之前,我希望得到一些帮助(非常欢迎链接)。
如果我走错了路,请告诉我。
最后,我想能够在我的应用程序中的任何地方做以下事情:
implode(', ', $array->linkifyArray())
以获得本问题开头的CCD_ 11的链接列表。
编辑:
我写了函数:
public function linkifyArray($array, $attributes) {
$htmlAttributes = '';
//inline attributes before appending them to <a></a>
if (is_array($attributes))
{
foreach ($attributes as $k => $v)
{
$htmlAttributes.= $k.'="'.$v.'" ';
}
}
$arrayOfLinks = [];
//create array of links
if(is_array($array))
{
foreach ($array as $kk => $vv)
{
$arrayOfLinks[]='<a '.$htmlAttributes.' href="'.$kk.'">'.$vv.'</a>';
}
}
return $arrayOfLinks;
}
并尝试在<?php
下面添加namespace App'Http;
,但我仍然收到内部服务器错误。
这就是我让它工作的方式。
app/Http/helpers.php
<?php
namespace app'Http;
class Helperfunctions {
/**
* Given the array of type ['slug' => 'title', ...]
* create new array of type [ '0' => '<a href='slug'>title</a>]
* if $attributes given (also array), implode them from ['id'=>'newLink', 'class'=>'newClass']
* to <a href='slug' id='newLink' class='newClass'>title</a>
*
*
* @param $array, $attributes, $prefix
* @return array
*/
public static function linkifyArray($array, $attributes, $prefix) {
$htmlAttributes = '';
//inline attributes before appending them to <a></a>
if (is_array($attributes))
{
foreach ($attributes as $k => $v)
{
$htmlAttributes.= $k.'="'.$v.'" ';
}
}
$arrayOfLinks = [];
//create array of links
if(is_array($array))
{
foreach ($array as $kk => $vv)
{
$arrayOfLinks[]='<a '.$htmlAttributes.' href="'.$prefix.$kk.'">'.$vv.'</a>';
}
}
return $arrayOfLinks;
}
}
添加到composer.json
autoload>classmap
:
"autoload": {
"classmap": [
"app/Http/helpers.php"
]
},
运行
composer dump-autoload
将视图中的函数调用为:
{!! implode(', ', 'app'Http'Helperfunctions::linkifyArray($myModel()->get()->lists('name', 'slug')->toArray(), [], '/prefix-uri/')) !!}
链接到我使用的教程。
我已经看到,您为自己制定了解决方案。但是,如果你不想用类来处理这类事情,你可以使用你最初答案的方法。唯一的问题是,在你的helpers.php文件中,你不能使用public等关键字
您必须将其视为一组未绑定在类中的函数。在类之外,public关键字会导致错误。
只需在helper.php中以以下方式定义您的函数:
<?php
function linkifyArray($array, $attributes, $prefix) {
// code
}
然后你可以在你的应用程序中访问它,只需输入(示例取自你的答案)
{!! implode(', ', linkifyArray($myModel()->get()->lists('name', 'slug')->toArray(), [], '/prefix-uri/')) !!}
public function linkifyArray($array, $attributes){
$arrayOfLinks = $array;
$html = "";
$id = "";
$class = "";
if($attributes['id'])
$id = $attributes['id'];
if($attributes['class'])
$class = $attributes['class'];
foreach($arrayOfLinks as $key=>$val)
{
$html.="<a href='".$key."' id='".$id."' class='".$class."'>".$val."</a>";
}
return $html;
}