I was looking through some inherited PHP code when I found the following:
$emails=array();
foreach($usrs as $usr){
$emails[]=$usr['email'];
}
It’s clear that this is trying to extract the ’email’ property of each object in a list of users and hold them in an array. That’s what I want it to do. Does this do that? I’ve never seen such a thing work this way. I replaced it with
array_push($emails, $usr['email']);
since I know that that does what I intend it to.
The two are the same, almost.
It will add an item to the end of the array, just as
array_pushdoes. The only difference isarray_pushreturns the new number of elements in the array. The empty bracket notation does not return anything, obviously.You should get comfortable with this notation, it’s easier to type and read.
array_pushon PHP docs mentions the use of this notation and covers two other differences as well.