views:

69

answers:

5

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 Index";  
$pages_array[0]->template = "interior";

$pages_array[1]->slug = "a";
$pages_array[1]->title = "100% Wide (Layout A)";
$pages_array[1]->template = "interior";

$pages_array[2]->slug = "homepage";
$pages_array[2]->title = "Homepage";
$pages_array[2]->template = "homepage";

I like how clearcut this is, but because I have to specify the index number, I can't rearrange them easily. How can I do this without the index number? Related, what is a better way to do this?

I also tried writing this by making a class, and having each spot on the array be instances of that class. But since this is a configuration file, it was hard to read and know what argument went with what parameter. That's why I chose this old-school method above.

Any thoughts are much appreciated!

+1  A: 

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.

Fanis
+1  A: 

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.

Samir Talwar
+3  A: 

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')
stereofrog
+1 for object casting. That's how I do it.
Ionuț G. Stan
A: 

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

NAVEED
A: 

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.

Aether
These are all awesome ideas, thanks everybody! Object casting and using a variable for the index are both great, simple solutions to my problem, much appreciated.
simpleguy