在PHP中,如何将对象元素添加到数组中?

时间:2021-08-12 03:19:26

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

我正在使用PHP。我有一个对象数组,并希望在它的末尾添加一个对象。

$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?

以上是功能性的,但是有更清晰,更易读的方式来编写吗?也许一行?

4 个解决方案

#1


49  

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) array('name' => 'My name');

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

但是,我认为即使它更简洁,也不是那么可读。

#2


5  

Do you really need an object? What about:

你真的需要一个物体吗?关于什么:

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

Just use a two-dimensional array.

只需使用二维数组。

Output (var_dump):

输出(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"];

#3


2  

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.

在这里,在调用array_filter $ myArray之前有3个元素。两个正确的对象和一个NULL。通话结束后,只有2个正确的元素持续存在。

#4


-1  

$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);

#1


49  

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) array('name' => 'My name');

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

但是,我认为即使它更简洁,也不是那么可读。

#2


5  

Do you really need an object? What about:

你真的需要一个物体吗?关于什么:

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

Just use a two-dimensional array.

只需使用二维数组。

Output (var_dump):

输出(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"];

#3


2  

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.

在这里,在调用array_filter $ myArray之前有3个元素。两个正确的对象和一个NULL。通话结束后,只有2个正确的元素持续存在。

#4


-1  

$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);