P
P
PanCar2021-06-30 18:08:46
PHP
PanCar, 2021-06-30 18:08:46

How to output objects from an array of objects in PHP?

Good day. I want to print the name of all the discord servers a user is a member of using the Discord API in PHP. But when I try to display the names echo $guildstest->name;It gives me an error Notice: Trying to get property of non-object in E:\OpenServer\domains\localhost\DSS\guilds.php on line 23 . I understand that this is an array, but I do not understand how then to display all this? I also tried it, it displays Notice: Undefined index: name in E:\OpenServer\domains\localhost\DSS\guilds.php on line 22 . There is nothing written about this in the official documentation. Please, help. Before asking a question, I googled, but I did not find what suited me. If something is not clear - write I will try to explain it more clearly. The array is here:echo $guildstest['name'];

array(2) {
  [0]=>
  object(stdClass)#1 (7) {
    ["id"]=>
    string(18) "389086811399520256"
    ["name"]=>
    string(29) "Дискорд Ютуберы"
    ["icon"]=>
    string(34) "a_5fce721c1032fbcd17d19ea60c86eaff"
    ["owner"]=>
    bool(false)
    ["permissions"]=>
    int(37045248)
    ["features"]=>
    array(14) {
      [0]=>
      string(15) "PREVIEW_ENABLED"
      [1]=>
      string(9) "COMMUNITY"
      [2]=>
      string(22) "WELCOME_SCREEN_ENABLED"
      [3]=>
      string(27) "ENABLED_DISCOVERABLE_BEFORE"
      [4]=>
      string(8) "COMMERCE"
      [5]=>
      string(24) "THREE_DAY_THREAD_ARCHIVE"
      [6]=>
      string(32) "MEMBER_VERIFICATION_GATE_ENABLED"
      [7]=>
      string(4) "NEWS"
      [8]=>
      string(15) "PRIVATE_THREADS"
      [9]=>
      string(13) "INVITE_SPLASH"
      [10]=>
      string(13) "ANIMATED_ICON"
      [11]=>
      string(24) "SEVEN_DAY_THREAD_ARCHIVE"
      [12]=>
      string(6) "BANNER"
      [13]=>
      string(12) "DISCOVERABLE"
    }
    ["permissions_new"]=>
    string(12) "109558711296"
  }
[1]=>
  object(stdClass)#2 (7) {
    ["id"]=>
    string(18) "855072618180116510"
    ["name"]=>
    string(27) "Сервер РЕКЛАМА"
    ["icon"]=>
    NULL
    ["owner"]=>
    bool(true)
    ["permissions"]=>
    int(2147483647)
    ["features"]=>
    array(0) {
    }
    ["permissions_new"]=>
    string(12) "137438953471"
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
John Didact, 2021-06-30
@PanCar

In any documentation, official or unofficial, this is written. For example, in the "arrays" and "objects" sections. At you objects lie in arrays under indexes. You need to access the property of the first object $arr[0]->name, the second one, $arr[1]->nameand so on. You can through a cycle, like

foreach($arr AS $obj){
 $obj->name;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question