Separate respective item from a JSON array

Asked

Viewed 265 times

0

all right? I wonder how do I 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 is 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 of returning, for example:

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

He 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 believe I’m missing something that I’m not concatenating on this line:

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

Anyone who can shed a light, I thank ^_^

  • Why was my question negative? I was editing it. I ended up sending it unintentionally before finishing the editing

1 answer

1


Just make it 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 mistakes is wanting to implode a link <a> removing a comma from each element. This does not work because the tag <a> will not be closed.

This implode along 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
  • 1

    Putz, man. That limps my huahuuah E nice this formula with for. Despite messing with PHP a long time ago, I still neglect some studies. I’ll study more about loppings to see if I don’t need to come here next time huahuahu Thank you very much!

  • @Alexlupóz kkk.. no problem.

  • ;) opa. Good luck to you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.