Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

object

php

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!

like image 919
dylanized Avatar asked Sep 14 '10 09:09

dylanized


People also ask

How do you declare an array of objects in PHP?

PHP allocates memory dynamically and what's more, it doesn't care what sort of object you store in your array. If you want to declare your array before you use it something along these lines would work: var $myArray = array(); Then you can store any object you like in your variable $myArray.

In what two ways can arrays in PHP be created?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

Can you have an array of objects in PHP?

We can use the array() function to create an array of objects in PHP. The function will take the object as the arguments and will create an array of those objects. We can create objects by creating a class and defining some properties of the class. The properties of the class will have some values.

Are PHP arrays zero Indexed?

PHP lets you create 2 types of array: Indexed arrays have numeric indices. Typically the indices in an indexed array start from zero, so the first element has an index of 0 , the second has an index of 1 , and so on. Usually, you use an indexed array when you want to store a bunch of data in a certain order.

How to create an array of objects in PHP?

We can use the array () function to create an array of objects in PHP. The function will take the object as the arguments and will create an array of those objects. We can create objects by creating a class and defining some properties of the class. The properties of the class will have some values.

Is it possible to define an array without an index?

if you dont put an index default is 0,1,2,3 n.... More sharing options... To answer your question seriously no. replacing your word "index" with php's term of key you can not define an array without having keys that have values under the scope of a single variable PHP is actually an ordered map. A map is a type that associates values to keys.

How to access the values of an array in PHP?

An array can hold many values under a single name, and you can access the values by referring to an index number. In PHP, the array () function is used to create an array: The count () function is used to return the length (the number of elements) of an array: For a complete reference of all array functions, go to our complete PHP Array Reference.

How many types of arrays are there in PHP?

In PHP, there are three types of arrays: 1 Indexed arrays - Arrays with a numeric index 2 Associative arrays - Arrays with named keys 3 Multidimensional arrays - Arrays containing one or more arrays


2 Answers

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')
like image 184
user187291 Avatar answered Oct 19 '22 11:10

user187291


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.

like image 17
Samir Talwar Avatar answered Oct 19 '22 11:10

Samir Talwar