0

How add a link using the id on comma separated multidimensional array?

What I done so far:

$string = implode(", ", array_column($title_genres, "name"));
echo $string;

My array:

array(4) {
  [0]=>
  object(stdClass)#66 (3) {
    ["id"]=>
    string(2) "21"
    ["name"]=>
    string(8) "Aventure"
    ["tmdb_id"]=>
    string(2) "12"
   }
  [1]=>
  object(stdClass)#67 (3) {
    ["id"]=>
    string(2) "20"
    ["name"]=>
    string(9) "Animation"
    ["tmdb_id"]=>
    string(2) "16"
   }
  [2]=>
  object(stdClass)#63 (3) {
    ["id"]=>
    string(1) "8"
    ["name"]=>
    string(8) "Familial"
    ["tmdb_id"]=>
    string(5) "10751"
   }
  [3]=>
  object(stdClass)#70 (3) {
    ["id"]=>
    string(1) "9"
    ["name"]=>
    string(11) "Fantastique"
    ["tmdb_id"]=>
    string(2) "14"
   }
}
AbraCadaver
  • 77,023
  • 7
  • 60
  • 83
Mappy Lord
  • 107
  • 1
  • 8

2 Answers2

2

I'd suggest a simple foreach on $title_genres to build a new array of just linked names, then you can do the implode on that to output with commas:

$links = array();
foreach($title_genres as $item) {
    $links[] = '<a href="/some.php?id='. $item['id'] .'">'. $item['name'] .'</a>';
}
echo implode(', ',$links);

Update 1: In case you are using stdClass objects:

$links = array();
foreach($title_genres as $item) {
    $links[] = '<a href="/some.php?id='. $item->id .'">'. $item->name .'</a>';
}
echo implode(', ',$links);

Update 2: An all in one line mash using array_map ;)

echo implode(', ', array_map(function($a){
                        return '<a href="/some.php?id='. $a->id .'">'. $a->name .'</a>';
                   },$title_genres) );
IncredibleHat
  • 3,992
  • 4
  • 14
  • 27
2

Here's the array_map solution, you decide if you consider it simpler than foreach.

$string = implode(",", array_map(function($item) {
    return "<a href='/some.php?id={$item->id'}'>{$item->name}</a>";
}, $title_genres));
echo $string;

If the base URL is in a variable, you can access it using a use() declaration.

$baseurl = base_url();
$string = implode(",", array_map(function($item) use ($baseurl) {
    return "<a href='$baseurl/some.php?id={$item->id'}'>{$item->name}</a>";
}, $title_genres));
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • Ah, heh, I just added one. But yeah! Refreshing to know I wasn't stabbing in the dark. (normally I **never** use array_map, I'm oldschool). – IncredibleHat Jan 11 '18 at 19:42
  • 1
    Forgot the variable with the array in it. – Barmar Jan 11 '18 at 19:46
  • @Barmar I choose your solutione, it's easier to write – Mappy Lord Jan 11 '18 at 19:49
  • How add a variable to array_map? – Mappy Lord Jan 11 '18 at 20:04
  • If you mean how to allow the function to access an outside variable, add the `use($variable)` declaration before the `{`. – Barmar Jan 11 '18 at 20:05
  • @Barmar Doesn't work, Can you give me an example please? – Mappy Lord Jan 11 '18 at 20:10
  • Can you explain what you're trying to do with the variable? – Barmar Jan 11 '18 at 20:11
  • I have a function call "base_url()" and several variables and I can't use them in array_map – Mappy Lord Jan 11 '18 at 20:14
  • I've added an example. – Barmar Jan 11 '18 at 20:15
  • You asked me about including a variable in the function. I assumed you would do `$baseurl = base_url();` before calling the function. – Barmar Jan 11 '18 at 20:22
  • @MappyLord If your base_url() function is outside a class, you can just use it inside the array_map anonymous function like you would anywhere else. If your base_url() is a method inside of a class, then here is more information on using methods inside the array_map anonymous function: https://stackoverflow.com/questions/19316623/call-class-method-from-inside-array-map-anonymous-function – IncredibleHat Jan 12 '18 at 15:15