Separate respective item from a JSON array

0

Alright? I would like to know how to add the respective value in "href" in this foreach:

<?php 
$getJson = file_get_contents('file.json');
$listMenuJson = json_decode($getJson, true);

foreach ($listMenuJson as $valor) {
$a_tag = implode('<a>', explode(',', ','.$valor['categorias_alt']));

echo '<div class="btn-group">';
    echo '<div class="dropdown">';
        echo '<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">'.$valor['titulo'];
        echo '<span class="caret"></span></button>';
        echo '<ul class="dropdown-menu">';
            echo '<li>';
                echo '<a href="'.$valor['codigo'].'">'.$valor['categorias'].'</a>';
            echo '</li>';
        echo '</ul>';
    echo '</div>';
echo '</div>';
}
?>

My JSON looks like this:

[
{
    "codigo":"termos",
    "titulo":"Termos Gerais",
    "categorias_alt":"Ephélian, Lentzh, Gourphour",
    "categorias":"ephelian, lentzh, gourphour"
},
{
    "codigo":"contos",
    "titulo":"Contos & Lendas",
    "categorias_alt":"Ephélian, Lentzh, Gourphour",
    "categorias":"ephelian, lentzh, gourphour"
},
{
    "codigo":"cabinet",
    "titulo":"Cabinet Escarlate",
    "categorias_alt":"Ephélian, Lentzh, Gourphour",
    "categorias":"ephelian, lentzh, gourphour"
}
]

In case, this part here:

echo '<a href="'.$valor['codigo'].'">'.$valor['categorias'].'</a>';

The system works, but instead returns, for example:

<a href="ephelian">Ephélian</a>
<a href="lentzh">Lentzh</a>
<a href="gourphour">Gourphour</a>

It returns this:

<a href="ephelian,lentzh,gourphour">Ephélian</a>
<a href="ephelian,lentzh,gourphour">Lentzh</a>
<a href="ephelian,lentzh,gourphour">Gourphour</a>

I think I'm letting something go that I'm not concatenating in this line:

$a_tag = implode('<a>', explode(',', ','.$valor['categorias_alt']));

Who can give a light, I thank ^ _ ^

    
asked by anonymous 13.05.2018 / 02:37

1 answer

1

It does so it will work:

$getJson = file_get_contents('file.json');
$listMenuJson = json_decode($getJson, true);

foreach ($listMenuJson as $valor) {
$categorias_alt = explode(',', $valor['categorias_alt']);
$categorias = explode(',', $valor['categorias']);

echo '<div class="btn-group">';
    echo '<div class="dropdown">';
        echo '<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">'.$valor['titulo'];
        echo '<span class="caret"></span></button>';
        echo '<ul class="dropdown-menu">';
            for($x = 0; $x < count($categorias_alt); $x++){
                echo '<li>';
                    echo '<a href="'.$categorias[$x].'">'.$categorias_alt[$x].'</a>';
                echo '</li>';
            }
        echo '</ul>';
    echo '</div>';
echo '</div>';
}

One of the errors is wanting to implode a <a> link by removing a comma from each element. This does not work because the <a> tag will not be closed.

This implode together with the explode returns something like this: Ephélian Lentzh Gourphour

$a_tag = implode('<a>', explode(',', ','.$valor['categorias_alt']));

Your code looks something like this:

<a>Ephélian <a>Lentzh <a>Gourphour // links que nunca se fecham
    
13.05.2018 / 17:33