32

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!

2
  • (sidenote) PHP has a native class for storing object collections with SplObjectStorage. If you need the objects in a specific order, have a look at SplMaxHeap Commented Sep 14, 2010 at 9:56
  • please clearify the question. What are you trying to achieve by "rearranging" and in general? Commented Sep 14, 2010 at 9:57

6 Answers 6

47

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, I did exactly this, getting a warning "PHP Warning: Creating default object from empty value in " How can I get around this ?
(object) array(...) this is what i looking for to make my code like a pro
19

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.

Comments

12

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"
    ),
   
);

Comments

4

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.

1 Comment

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.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.