0

I am trying to insert some items (suppose n items), which are all different from each other, to an array. Somehow, the final array consists of n items, Which all are the same item: the last inserted item.

This is my code:

$searchResults_data = [];
foreach($allowSearch as $searchResultItem) {
    $searchResultJSon->dealid = $searchResultItem['id'];
    $searchResultJSon->title = $searchResultItem['title'];
    //$from->send(json_encode($searchResultJSon)); --- DEBUGGING1 ---
    //$from->send(json_encode($searchResults_data)); --- DEBUGGING2 ---
    $searchResults_data[] =  $searchResultJSon;
}

So I tried to figure out why is that.. using DEBUGGING1,DEBUGGING2 (in the client side, I get the messages sent by $from->send() and simply alert() them).

When alerting the DEBUGGING1 messages - I do see that all the items are correct and different from each other.

When alerting the DEBUGGING2 messages - the array duplicates the last inserted item each loop. So assume I insert n items, The array in the i-th loop will be: [item-i, item-i, item-i, ... item-i] instead of [item-1, item-2, item-3,...,item-i]

2
  • Where is $searchResultJSon originally defined? Commented Feb 5, 2020 at 8:17
  • @Nick nowhere. Only in the foreach loop without pre-declaration Commented Feb 5, 2020 at 8:19

1 Answer 1

2

Your problem is that you're not creating a new object each time you go through the loop, so when you push $searchResultJSon into $searchResults_data you are pushing the same object, and the changes you make to it in the last iteration of the loop are reflected in all the values in $searchResults_data. You can work around that by creating a new object in each pass:

$searchResults_data = [];
foreach($allowSearch as $searchResultItem) {
    $searchResultJSon = new StdClass();
    $searchResultJSon->dealid = $searchResultItem['id'];
    $searchResultJSon->title = $searchResultItem['title'];
    //$from->send(json_encode($searchResultJSon)); --- DEBUGGING1 ---
    //$from->send(json_encode($searchResults_data)); --- DEBUGGING2 ---
    $searchResults_data[] =  $searchResultJSon;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why a new object is required? Why updating its fields not working?
@QwertTrewq because when you push an object into an array, you only push a reference to the object, not a copy of it. So your array fills with references to the same object, and when you change any copy of the object you change all the references as well. By creating a new object each time through the loop you avoid that problem.
@QwertTrewq I made a small demo here 3v4l.org/eX4Zl note in the first block that even without pushing the object into the array, just changing its values affect the values of the references in the array.

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.