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

后端 未结 6 1936
北海茫月
北海茫月 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 13:05

    If you make them arrays with named keys rather than objects, you can do it like this:

    $pages_array = array(
         array(
             'slug' => 'index',
             'title' => 'Site Index',
             'template' => 'interior'
         ),
    
         array(
             'slug' => 'a',
             'title' => '100% Wide (Layout A)',
             'template' => 'interior'
         ),
    
         array(
             'slug' => 'homepage',
             'title' => 'Homepage',
             'template' => 'homepage'
         )
    );
    

    You can combine this with Fanis' solution and use the slugs as the keys if you like.

提交回复
热议问题