Created
July 3, 2021 19:41
-
-
Save AhmedHelalAhmed/16e6e6ce46b1e641128d919fab672a4b to your computer and use it in GitHub Desktop.
array of objects vs array of arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 10 | |
// 20 | |
// 20 | |
// 40 | |
dd(collect([ | |
[ | |
'id' => 1, | |
'price' => 10 | |
], | |
[ | |
'id' => 1, | |
'price' => 20 | |
], | |
])->each(function($item){ | |
$item['price']=$item['price']*2; | |
return $item; | |
}),collect([ | |
(object)[ | |
'id' => 1, | |
'price' => 10 | |
], | |
(object)[ | |
'id' => 1, | |
'price' => 20 | |
], | |
])->each(function($item){ | |
$item->price=$item->price*2; | |
return $item; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/2030906/are-arrays-in-php-copied-as-value-or-as-reference-to-new-variables-and-when-pas