0

I use something like

$data = [];
foreach (){
  $data[] = $api_content;
}
return $data;

the $data array does not get overwritten. Instead the values of $api_content get appended.

Is that right??? Why? I thought it gets overwritten....

user1664377
  • 393
  • 1
  • 3
  • 13
  • 2
    You are adding to the array - https://stackoverflow.com/questions/676677/how-to-add-elements-to-an-empty-array-in-php – Nigel Ren Feb 19 '22 at 12:40
  • 1
    It is syntactic sugar for array_push (see https://www.php.net/manual/en/function.array-push.php). – lukas.j Feb 19 '22 at 12:49
  • 1
    `if no key is specified, the maximum of the existing int indices is taken, and the new key will be that maximum value plus 1 (but at least 0). If no int indices exist yet, the key will be 0 (zero).` see https://www.php.net/manual/en/language.types.array.php – user3783243 Feb 19 '22 at 13:06
  • 1
    `$data[] = $api_content` appends, whereas `$data = $api_content` overwrites, and you lose the array structure in the process. – GetSet Feb 19 '22 at 17:30

0 Answers0