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

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

    Assuming you're not setting any $pages_array elements elsewhere, you can set a simple variable.

    $i = 0;
    
    $pages_array[$i]->slug = "index";
    $pages_array[$i]->title = "Site Index";  
    $pages_array[$i]->template = "interior";
    $i++;
    
    $pages_array[$i]->slug = "a";
    $pages_array[$i]->title = "100% Wide (Layout A)";
    $pages_array[$i]->template = "interior";
    $i++;
    
    $pages_array[$i]->slug = "homepage";
    $pages_array[$i]->title = "Homepage";
    $pages_array[$i]->template = "homepage";
    $i++;
    

    You just have to remember to increment $i every time so you don't overwrite an element.

    0 讨论(0)
  • 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')
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 13:01

    There are three ways:

    Way 1 :

    $pages_array=[
        [
            "id" => 1,
            "name" => "James"
        ],
        [
            "id" => 2,
            "name" => "Mary"
        ]
    
    ];
    

    Way 2 :

     $pages_array[0] =
        [
            "id" => 1,
            "name" => "James"
        ];
        $pages_array[1] = [
            "id" => 2,
            "name" => "Mary"
        ];
    

    Way 3 :

    $pages_array = array(
        array(
          "id" => 2,
          "name" => "Mary"
        ),
    
        array(
          "id" => 1,
          "name" => "James"
        ),
       
    );
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 13:05

    If you want to maintain sequence indexes. May be this will help.

    $pages_array[]->slug = "index";
    $key = array_search(end($pages_array),$pages_array );
    $pages_array[$key]->title = "Site Index";  
    $pages_array[$key]->template = "interior";
    
    $pages_array[]->slug = "a";
    $key = array_search(end($pages_array),$pages_array );
    $pages_array[$key]->title = "100% Wide (Layout A)";
    $pages_array[$key]->template = "interior";
    
    $pages_array[]->slug = "homepage";
    $key = array_search(end($pages_array),$pages_array );
    $pages_array[$key]->title = "Homepage";
    $pages_array[$key]->template = "homepage";
    

    Check

    0 讨论(0)
提交回复
热议问题