Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make each object unique when creating an array of objects using array_fill?

Tags:

arrays

object

php

I'm trying to create an array of objects in Yii2. However, the problem is that the objects within the array have absolutely no distinction. Here's what I'm trying:

$array1 = array(new Object, new Object, new Object);
$array2 = array_fill(0, 2, new Object);

At first glance, they look the same, but the outputs are not.

$array1

[
    0 => frontend\models\Object#1
    (
        [yii\db\BaseActiveRecord:_attributes] => []
        [yii\db\BaseActiveRecord:_oldAttributes] => null
        [yii\db\BaseActiveRecord:_related] => []
        [yii\base\Model:_errors] => null
        [yii\base\Model:_validators] => null
        [yii\base\Model:_scenario] => 'default'
        [yii\base\Component:_events] => []
        [yii\base\Component:_behaviors] => []
    )
    1 => frontend\models\Object#2
    (
        [yii\db\BaseActiveRecord:_attributes] => []
        [yii\db\BaseActiveRecord:_oldAttributes] => null
        [yii\db\BaseActiveRecord:_related] => []
        [yii\base\Model:_errors] => null
        [yii\base\Model:_validators] => null
        [yii\base\Model:_scenario] => 'default'
        [yii\base\Component:_events] => []
        [yii\base\Component:_behaviors] => []
    )
    2 => frontend\models\Object#3
    (
        [yii\db\BaseActiveRecord:_attributes] => []
        [yii\db\BaseActiveRecord:_oldAttributes] => null
        [yii\db\BaseActiveRecord:_related] => []
        [yii\base\Model:_errors] => null
        [yii\base\Model:_validators] => null
        [yii\base\Model:_scenario] => 'default'
        [yii\base\Component:_events] => []
        [yii\base\Component:_behaviors] => []
    )
]

And here is $array2

[
    1 => frontend\models\Object#4
    (
        [yii\db\BaseActiveRecord:_attributes] => []
        [yii\db\BaseActiveRecord:_oldAttributes] => null
        [yii\db\BaseActiveRecord:_related] => []
        [yii\base\Model:_errors] => null
        [yii\base\Model:_validators] => null
        [yii\base\Model:_scenario] => 'default'
        [yii\base\Component:_events] => []
        [yii\base\Component:_behaviors] => []
    )
    2 => frontend\models\Object#4(...)
    3 => frontend\models\Object#4(...)
]

Notice the number after "Object" doesn't change in $array2 like it does in $array1. This is causing some problems for my code. I know I can simply do array_push but this particular array could grow to be quite large and I'd rather not use a loop to create it. If it can be done in a single command, I'd really like to use that method.

like image 583
MikelG Avatar asked Jan 28 '15 23:01

MikelG


People also ask

What method would create new array that will have the same length of the original array?

Array.prototype.map() prototype. map() creates a new array by applying the provided transformation to each element of the original array. The result is an array with the same length as the original array and elements transformed based on the provided function.

How do you fill an array of objects?

In JavaScript, you can use the Array. fill() method to populate an array with a zero or any other value like an object or a string. This method replaces all elements in an array with the value you want to populate the array with and returns the modified array.

How do you populate an array in JavaScript?

Array.fill For example, if we want to set up an array with ten slots and populate it with the string “hello” we'd write some code like this: let filledArray = new Array(10). fill('hello'); This method works great for immutable values like numbers, strings, and booleans.

When assigning a value to an array without specifying the key value What will the key be assigned as?

Note: As mentioned above, if no key is specified, the maximum of the existing int indices is taken, and the new key will be that maximum value plus 1 (but at least 0). If no int indices exist yet, the key will be 0 (zero). // Create a simple array.


1 Answers

As far as I'm concerned, and as far as you told, the cleaner way of achieving this is still a simple loop:

$n = 56; 
for ($i = 0; $i < $n; $i++) $array[] = new Object;

It is one line solution, and I think it works quite fast... There are many functions for operating on each element of array, but almost all of them works in the way You described. So why not to use simple for loop?

Even if the array is quite big (as you said), there is nothing wrong with this solution I think. It is one line and elegant, and clean. Even when the nerds at http://www.php.net are doing everything they can to make the built in functions work fast, I dont think that any of the build in functions (even if exists) should do it really faster. I didn't test the speed, but I'm quite sure there will be almost no difference.

However your question is very good, and thanks to it, I read all the array functions in manual on php.net: http://php.net/manual/en/ref.array.php


I think the code is clean enough, but I will describe it for others:

$array[] = new Object;

This appends new Object at the end of an array.

In that case, it gives an array that is filled with new Object from 0 to 55, for example:

array[10] // -> some object; 
array[35] // -> another object;

PS. Single command (as you said) just look nice. It doesn't work like a magic command, and almost always there is a huge amount of code "under it". The only way, using built in functions is better, it is that almost always this prepared "under code" is faster then yours.

That's why I recommend on going with this easy readable solution, because all array walk functions, etc, have also many, many lines of complicated code inside :). Which method is faster, only a benchmark can tell, but I can bet that for up to 1000 objects, the difference will be "almost none".


And as far, as there is no dedicated function for that particular example, you must use some "elegant trick" on one of the functions that are optimized for doing something... Let's say else. This function can work much more slower in that case.

like image 116
Jacek Kowalewski Avatar answered Sep 24 '22 14:09

Jacek Kowalewski