Home > OS >  Multidimensional PHP Array - Multiple
Multidimensional PHP Array - Multiple

Time:07-10

I have the following array that prints out using the code $users. However, What I want to do is just have an array with all the user>usernames and not all the other information.

    Array
(
    [0] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-05-06T15:22:36.860000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 140976603248328705
                    [username] => Agent Lemonfoot
                    [avatar] => 53afebc2155602989fae62e388aa171f
                    [avatar_decoration] => 
                    [discriminator] => 6048
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [1] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-06-07T22:24:30.245000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 146707757058359296
                    [username] => thecowboy31
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 3395
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [2] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-10-06T19:05:24.342000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 215314334488854528
                    [username] => dan fantom
                    [avatar] => 9e18411ddbdbb057dce7e25316a1a207
                    [avatar_decoration] => 
                    [discriminator] => 8216
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [3] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-11-17T06:08:15.126000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 584040955670167560
                    [username] => vincentdevoll
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 4739
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [4] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-11-18T08:40:13.525000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 689056243943604226
                    [username] => Ralph12Rose
                    [avatar] => ffbb8100abb2f70d6e78e9e9686cbd30
                    [avatar_decoration] => 
                    [discriminator] => 7291
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [5] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-03-28T02:31:35.679000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 741306918282461245
                    [username] => HaLlMd
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 6350
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [6] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-01-10T05:23:45.369000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 778285620811268097
                    [username] => Matxzl ඞ
                    [avatar] => eb3de5d9157cf6fe064541fbc23bd4fe
                    [avatar_decoration] => 
                    [discriminator] => 1466
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [7] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-05-04T17:08:34.037000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 812721021882073089
                    [username] => Croesus
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 0822
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [8] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-05-10T22:28:51.298000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 841439325480026152
                    [username] => ...James
                    [avatar] => 2c71adf384779b8a5cea25a6e7c76295
                    [avatar_decoration] => 
                    [discriminator] => 3515
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [9] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-05-12T16:53:32.792000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 842081694039408710
                    [username] => ShelbyB18
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 9671
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [10] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-08-23T08:26:27.229000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 879280326092914698
                    [username] => Harly
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 5731
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [11] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-09-20T20:48:34.762000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 889613843746132058
                    [username] => Lapdog
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 7648
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [12] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2021-11-13T22:27:16.191000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 909207709314846820
                    [username] => bgfgf
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 8789
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [13] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-05-07T00:54:09.916000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 910390413515456522
                    [username] => Rese100
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 9933
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [14] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-05-30T22:36:50.522000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 926447674490753056
                    [username] => m.r.idek
                    [avatar] => 79ee349b6511e2000af8a32fb8a6974e
                    [avatar_decoration] => 
                    [discriminator] => 5631
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [15] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-02-27T04:52:12.182000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 947354968137019462
                    [username] => Xcane
                    [avatar] => efa9eaa105b8535f27ff2cd5b7c188f7
                    [avatar_decoration] => 
                    [discriminator] => 3459
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [16] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-05-08T14:44:57.558000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                )

            [user] => stdClass Object
                (
                    [id] => 972871467073151056
                    [username] => Brezwright
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 4474
                    [public_flags] => 0
                )

            [mute] => 
            [deaf] => 
        )

    [17] => stdClass Object
        (
            [avatar] => 
            [communication_disabled_until] => 
            [flags] => 0
            [is_pending] => 
            [joined_at] => 2022-07-07T19:18:43.954000 00:00
            [nick] => 
            [pending] => 
            [premium_since] => 
            [roles] => Array
                (
                    [0] => 994683907582726187
                )

            [user] => stdClass Object
                (
                    [id] => 994675389899223191
                    [username] => CashbackGod
                    [avatar] => 
                    [avatar_decoration] => 
                    [discriminator] => 4556
                    [public_flags] => 0
                    [bot] => 1
                )

            [mute] => 
            [deaf] => 
        )

)

I have tried the following, but this brings back just the first bit of data from [0], and nothing from user

$keys = array_keys($users);
for($i = 0; $i < count($users); $i  ) {
    echo $keys[$i] . "{<br>";
    foreach($users[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "}<br>";
}

CodePudding user response:

You just need a single foreach loop.

$usernames = [];
foreach($users as $user) {
    $usernames[$user->id] = $user->username;
}

then $usernames will be a key->value array where the key is the users ID and the value is the users username.

  • Related