I need to create an array in Php by using index as nth number. For instance:
<?php
$A = array();
$A['1000']='Some value';
Does this array will occupy memory for remaining 999 indexes?
An array in php associates values to keys. It is like an ordered map as you can find in the official documentation.
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
This means that php won't allocate memory for the remaining 999 indexes. Also, you can test it by converting the array to json:
echo json_encode($A);
This will return
{"1000":"Some value"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With