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!
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.
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.
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.
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.
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.
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.
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.
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
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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With