how to build arrays of objects in PHP without specifying an index number?

后端 未结 6 1931
北海茫月
北海茫月 2021-01-01 12:07

Here is a weird question. I am building an array of objects manually, like this:

$pages_array[0]->slug = \"index\";
$pages_array[0]->title = \"Site Ind         


        
6条回答
  •  离开以前
    2021-01-01 12:55

    This code

     $pages_array[1]->slug = "a";
    

    is invalid anyways - you'll get a "strict" warning if you don't initialize the object properly. So you have to construct an object somehow - either with a constructor:

     $pages_array[] = new MyObject('index', 'title'....)
    

    or using a stdclass cast

     $pages_array[] = (object) array('slug' => 'xxx', 'title' => 'etc')
    

提交回复
热议问题