Is there a simple way to build a list of links from a 1D array?
For example:
$array = [
'slug-1' => 'Title 1',
'slug-2' => 'Title 2',
'slug-3' => 'Title 3'
]
echoed as: <a href="slug-1">Title 1</a>, <a href="slug-2">Title 2</a>, <a href="slug-3">Title 3</a>
Basicly a modified implode function with possibility of adding extra parameters as class="something" and similar.
I doubt there is so this is what I'm trying to do.
Create global functions:
- create
app/Http/helpers.php - edit
composer.jsonby adding"files": ["app/Http/helpers.php"]inautoloadblock - run
composer dump-autoload
as described here.
Not sure how to proceed from here. I tried adding following code to the 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;
}
This does nothing but I tried to explain what I'm trying to do in the comments.
I believe I could create this function by myself but when I copy the code above to helpers.php file I'm getting internal server error and PHPStorm is telling me he's expecting statement.
So I'm missing something very basic here and would like some help (links are most welcome) before I can proceed to create my helper function.
Please tell me if I'm approaching this the wrong way.
In the end I want to be able to do following wherever in my app:
implode(', ', $array->linkifyArray())
to get list of links from $array in the beginning of this question.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire