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

后端 未结 6 1950
北海茫月
北海茫月 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:00

    Assuming the slug will be unique, perhaps use it to identify your page arrays:

    $pages_array = array() ;
    
    $pages_array['index'] = array() ;
    $pages_array['index']['title'] = 'Site Index' ;
    $pages_array['index']['template'] = 'interior' ;
    
    $pages_array['a'] = array() ;
    $pages_array['a']['title'] = '100% Wide (Layout A)' ;
    $pages_array['a']['template'] = 'interior' ;
    

    etc

    You'll just need to use the array key to get your slug value.

提交回复
热议问题