71

I'm using PHP. I have an array of objects, and would like to add an object to the end of it.

$myArray[] = null; //adds an element
$myArray[count($myArray) - 1]->name = "my name"; //modifies the element I just added

The above is functional, but is there a cleaner and more-readable way to write that? Maybe one line?

3
  • 3
    There's no way for this to be functional. It should throw an error: Accessing property of a null object at line 2 in stackoverflow example Commented Jan 28, 2013 at 22:25
  • 3
    @sinistraD - PHP doesn't care that it's null. It'll cast it to an object and set the property, and emit a warning. Commented Jan 28, 2013 at 22:34
  • This will throw a null object warning. Commented Oct 24, 2017 at 14:41

4 Answers 4

138

Just do:

$object = new stdClass();
$object->name = "My name";
$myArray[] = $object;

You need to create the object first (the new line) and then push it onto the end of the array (the [] line).

You can also do this:

$myArray[] = (object) ['name' => 'My name'];

However I would argue that's not as readable, even if it is more succinct.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for being more readable, but it's now THREE lines. I can't create the object within a declaration?
I think @nickb is right, you can create objects implictly, but I don't recommend it as it will probably raise a warning when you do.
I think you can cast to (object) to PHP, even if the intent of the code is less clear. See my answer edit.
@adamdport It depends on the definition of your class. If it takes a name in its constructor, you could do: $myArray[] = new MyClass("My name"); But some objects won't be able to have all data set in the constructor. Then it's easier to create the object, set the parameters and then add it.
42

Here is a clean method I've discovered:

$myArray = [];

array_push($myArray, (object)[
        'key1' => 'someValue',
        'key2' => 'someValue2',
        'key3' => 'someValue3',
]);

return $myArray;

Comments

9

Do you really need an object? What about:

$myArray[] = array("name" => "my name");

Just use a two-dimensional array.

Output (var_dump):

array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(7) "my name"
  }
}

You could access your last entry like this:

echo $myArray[count($myArray) - 1]["name"];

2 Comments

When I try to access $myArray[0][0] I get an error.
@MartinStein in the example above, you would use $myArray[0]["name"] because the second item it an object.
4

Something like:

class TestClass {
private $var1;
private $var2;

private function TestClass($var1, $var2){
    $this->var1 = $var1;
    $this->var2 = $var2;
}

public static function create($var1, $var2){
    if (is_numeric($var1)){
        return new TestClass($var1, $var2);
    }
    else return NULL;
}
}

$myArray = array();
$myArray[] = TestClass::create(15, "asdf");
$myArray[] = TestClass::create(20, "asdfa");
$myArray[] = TestClass::create("a", "abcd");

print_r($myArray);

$myArray = array_filter($myArray, function($e){ return !is_null($e);});

print_r($myArray);

I think that there are situations where this constructions are preferable to arrays. You can move all the checking logic to the class.

Here, before the call to array_filter $myArray has 3 elements. Two correct objects and a NULL. After the call, only the 2 correct elements persist.

5 Comments

I recommend reading up on constructors (function __construct). They are PHPs built in way of doing what you're doing in your example!
@Erk: the method private function TestClass does the same thing, it's the old way in PHP of setting up a constructor (from PHP 4). From PHP 5, your way is preferred.
@halfer, I had a period this spring when I thought I understood PHP. I have since then realized I do not :D
@Erk: it is a never-ending journey, with any language :-).
@halfer, you're right, of course, but PHP is the first language I've come a cross that IS and IS NOT at the same time. I crashed and burned on pointers that PHP HAS and HAS NOT, so now I'm running back to C# and Java :)

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.