0

I have the following PHP array:

stdClass::__set_state(array(
   'channels' => 
  stdClass::__set_state(array(
     'INFORMATIONEN' => 
    array (
      0 => '👋welcome',
      1 => '📜rules',
      2 => '📞contact',
    ),
     'NEWS' => 
    array (
      0 => '✉news',
      1 => '🎥videos',
    ),
     'TEAM' => 
    array (
      0 => '♠team-lounge',
      1 => '💻coding',
      2 => 'bottest',
      3 => '📈Besprechung',
      4 => '♠Team Lounge'
    ),
     'LIVE' => 
    array (
      0 => '🎥Live Stream 1',
      1 => '🎥Live Stream 2',
    ),
     'RECORD' => 
    array (
      0 => '🎥Aufnahme 1',
      1 => '🎥Aufnahme 2',
    ),
     'TK' => 
    array (
      0 => 'Diff',
    ),
     'USER' => 
    array (
      0 => '🧉lounge-1',
      1 => '🧉lounge-2',
      2 => '🧉lounge-3',
      3 => '🧉Lounge 1',
      4 => '🧉Lounge 2',
      5 => '🧉Lounge 3',
    ),
     'Support' => 
    array (
      0 => 'supportticket',
    ),
     'AFK' => 
    array (
      0 => '🛌AFK',
    ),
  )),
))

Thats the output of the following command:

echo '<pre>' . var_export($decodedChannels, true) . '</pre>';

That's a channel list of a discord server, which I'd like to display on a website.

What it should be displayed like is basically as a nested unordered list something like:

<ul>
    <li>INFORMATIONEN</li>
    <li>
        <ul>
            <li>Welcome</li>
            <li>rules</li>
            <li>contact</li>
        </ul>
    </li>
    <li>NEWS</li>
    <li>
        <ul>
            <li>News</li>
            <li>Videos</li>
        </ul>
    </li>
</ul>

And so on for the other array parts of course. Basically it should look like in discord...

I searched a lot and found various solutions with foreach loops, different ideas and everything, but nothing worked... Either I only got the first layer of the array or only the keys or other issues...

Can anybody point me to the right idea to solve the problem?

4
  • The magic word is recursion. Commented Dec 10, 2020 at 9:44
  • @MarkusZeller yes, I've tried that, like here stackoverflow.com/a/24865996/3375021 or here stackoverflow.com/a/6260587/3375021... but it doesn't work for me... Commented Dec 10, 2020 at 9:45
  • What have you tried, what doesnt work for you? You literally have to copy the function ToUl as mentioned in your link and pass the array to the function. It creates everything except one additional li for like 'INFORMATIONEN'. Commented Dec 10, 2020 at 9:52
  • @DefinitelynotRafal the only result I get copy pasting theToUl function is the number 1.. nothing else.. no elements at all...for whatever reason..(inside a ul, without an li) Commented Dec 10, 2020 at 10:01

1 Answer 1

1

You can use recursion to nest it. You have some unusual requirement of title listing, but this should work as you expect.

$data = array(
    'INFORMATIONEN' =>
        array(
            0 => '👋welcome',
            1 => '📜rules',
            2 => '📞contact',
        ),
    'NEWS'          =>
        array(
            0 => '✉news',
            1 => '🎥videos',
        ),
    'TEAM'          =>
        array(
            0 => '♠team-lounge',
            1 => '💻coding',
            2 => 'bottest',
            3 => '📈Besprechung',
            4 => '♠Team Lounge'
        ),
    'LIVE'          =>
        array(
            0 => '🎥Live Stream 1',
            1 => '🎥Live Stream 2',
        ),
    'RECORD'        =>
        array(
            0 => '🎥Aufnahme 1',
            1 => '🎥Aufnahme 2',
        ),
    'TK'            =>
        array(
            0 => 'Diff',
        ),
    'USER'          =>
        array(
            0 => '🧉lounge-1',
            1 => '🧉lounge-2',
            2 => '🧉lounge-3',
            3 => '🧉Lounge 1',
            4 => '🧉Lounge 2',
            5 => '🧉Lounge 3',
        ),
    'Support'       =>
        array(
            0 => 'supportticket',
        ),
    'AFK'           =>
        array(
            0 => '🛌AFK',
        ),
);

function listArray($array) {
    echo "<ul>\n";
    foreach($array as $key => $value) {
        echo "<li>";
        if(is_array($value)) {
            listArray($value);
        } else {
            echo $value;
        }
        echo "</li>\n";
    }
    echo "</ul>\n";
}

foreach($data as $key => $array) {
    echo "<ul>\n";
    echo "<li>$key</li>\n";
    echo "<li>";
    listArray($array);
    echo "</li>\n";
    echo "</ul>\n";
}

This gives this HTML output

<ul>
    <li>INFORMATIONEN</li>
    <li>
        <ul>
            <li>👋welcome</li>
            <li>📜rules</li>
            <li>📞contact</li>
        </ul>
    </li>
</ul>
<ul>
    <li>NEWS</li>
    <li>
        <ul>
            <li>✉news</li>
            <li>🎥videos</li>
        </ul>
    </li>
</ul>
<ul>
    <li>TEAM</li>
    <li>
        <ul>
            <li>♠team-lounge</li>
            <li>💻coding</li>
            <li>bottest</li>
            <li>📈Besprechung</li>
            <li>♠Team Lounge</li>
        </ul>
    </li>
</ul>
<ul>
    <li>LIVE</li>
    <li>
        <ul>
            <li>🎥Live Stream 1</li>
            <li>🎥Live Stream 2</li>
        </ul>
    </li>
</ul>
<ul>
    <li>RECORD</li>
    <li>
        <ul>
            <li>🎥Aufnahme 1</li>
            <li>🎥Aufnahme 2</li>
        </ul>
    </li>
</ul>
<ul>
    <li>TK</li>
    <li>
        <ul>
            <li>Diff</li>
        </ul>
    </li>
</ul>
<ul>
    <li>USER</li>
    <li>
        <ul>
            <li>🧉lounge-1</li>
            <li>🧉lounge-2</li>
            <li>🧉lounge-3</li>
            <li>🧉Lounge 1</li>
            <li>🧉Lounge 2</li>
            <li>🧉Lounge 3</li>
        </ul>
    </li>
</ul>
<ul>
    <li>Support</li>
    <li>
        <ul>
            <li>supportticket</li>
        </ul>
    </li>
</ul>
<ul>
    <li>AFK</li>
    <li>
        <ul>
            <li>🛌AFK</li>
        </ul>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer.. for some reason however, I don't get the keys displayed, so I only get the sub elements but not the parents (the categories from discord)... also, on the very top, I get "channels", because its the root of the array, can I somehow grab everything underneath that?
Try $data = $yourObject->channels. I could not rebuild the stdClass, so I converted to array for convinience. You also can cast object to array like $data = (array)$yourObject; var_dump($data['channels']).
yep, thats it! :) Thanks a lot! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.