PHP array_push() 正在覆盖现有的数组元素 [英] PHP array_push() is overwriting existing array elements

查看:52
本文介绍了PHP array_push() 正在覆盖现有的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对象推送到数组中,以得到这样的对象数组:

<预><代码>[{"recipient_name":"John D", "phone_number":"123456"},{"recipient_name":"Doe J", "phone_number":"654321"},{"recipient_name":"Jon Do", "phone_number":"112233"},]

所以我循环遍历一个更大的数组来获取姓名和电话号码,并将它们作为一个对象推送到这样的数组中:

$myLargerArray = pg_fetch_all($messageQuery);//这是我更大的数组$size = count($myLargerArray);for( $j = 0; $j <$size; $j++ ) {$myRecipientsObj->recipient_name = $myLargerArray[$j]['recipient_name'];$myRecipientsObj->phone_number = $myLargerArray[$j]['phone_number'];var_dump($myRecipientsObj);//这会输出从 [$j] 添加的正确数据array_push($myObjArray->message_recipients, $myRecipientsObj);var_dump($myObjArray->message_recipients);//输出显示数组元素在每次循环迭代时被覆盖}

这是最后一个 var_dump($myObjArray->message_recipients) 的示例:

array(1) {[0]=>对象(标准类)#2(2){[收件人姓名"]=>string(12) "第一人称"[电话号码"]=>字符串(9)112233445"}}数组(2){[0]=>对象(标准类)#2(2){[收件人姓名"]=>string(13) "第二人称"[电话号码"]=>字符串(9)123456789"}[1]=>对象(标准类)#2(2){[收件人姓名"]=>string(12) "第二人称"[电话号码"]=>字符串(9)123456789"}}数组(3){[0]=>对象(标准类)#2(2){[收件人姓名"]=>string(16) "第三人称"[电话号码"]=>字符串(9)012345678"}[1]=>对象(标准类)#2(2){[收件人姓名"]=>string(16) "第三人称"[电话号码"]=>字符串(9)012345678"}[2]=>对象(标准类)#2(2){[收件人姓名"]=>string(16) "第三人称"[电话号码"]=>字符串(9)012345678"}}数组(4){...//它只是用重复的数据覆盖数据}

我做错了什么导致这种情况,我该如何解决?

解决方案

那是因为当你使用:

array_push($myObjArray->message_recipients, $myRecipientsObj);

您正在将对该对象的引用推送到数组中,然后当您在随后的循环中更改对象时,您实际上更改了数组每个元素的内容.要解决此问题,您需要将对象的副本推送到数组中:

array_push($myObjArray->message_recipients, clone $myRecipientsObj);

3v4l.org 上的演示

I'm trying to push objects into an array to end up with an object array like this:

[
    {"recipient_name":"John D", "phone_number":"123456"},
    {"recipient_name":"Doe J", "phone_number":"654321"},
    {"recipient_name":"Jon Do", "phone_number":"112233"},
]

So I'm looping over a larger array to acquire the names and phone numbers and pushing them as an object to an array like this:

$myLargerArray = pg_fetch_all($messageQuery); // This is my larger array

$size = count($myLargerArray);
for( $j = 0; $j < $size; $j++ ) {
    $myRecipientsObj->recipient_name = $myLargerArray[$j]['recipient_name'];
    $myRecipientsObj->phone_number = $myLargerArray[$j]['phone_number'];

    var_dump($myRecipientsObj); // This outputs the correct data added from [$j]

    array_push($myObjArray->message_recipients, $myRecipientsObj);

    var_dump($myObjArray->message_recipients); // The output shows array elements are being overwritten at each loop iteration
}

This is an example of how the last var_dump($myObjArray->message_recipients) looks like:

array(1) {
  [0]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(12) "First Person"
    ["phone_number"]=>
    string(9) "112233445"
  }
}
array(2) {
  [0]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(13) "Second Person"
    ["phone_number"]=>
    string(9) "123456789"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(12) "Second Person"
    ["phone_number"]=>
    string(9) "123456789"
  }
}
array(3) {
  [0]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(16) "Third Person"
    ["phone_number"]=>
    string(9) "012345678"
  }
  [1]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(16) "Third Person"
    ["phone_number"]=>
    string(9) "012345678"
  }
  [2]=>
  object(stdClass)#2 (2) {
    ["recipient_name"]=>
    string(16) "Third Person"
    ["phone_number"]=>
    string(9) "012345678"
  }
}
array(4) {
  ... // it just overwriting the data with duplicates
}

What am I doing wrong to cause this and how can I get around it?

解决方案

It's because when you use:

array_push($myObjArray->message_recipients, $myRecipientsObj);

you are pushing a reference to the object into the array, and when you then change the object in subsequent passes through the loop, you effectively change the contents of each element of the array. To work around this, you need to push a copy of the object into the array instead:

array_push($myObjArray->message_recipients, clone $myRecipientsObj);

Demo on 3v4l.org

这篇关于PHP array_push() 正在覆盖现有的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆