Are numeric keyed arrays automatically sorted, even if they are not created in order?
I want to know, if set an array, using a variable which contains integers (let’s say from 0-9) for a key but the variable doesn’t range aren’t placed in order, shouldn’t the array doesn’t var_dump in numerical order of it’s keys…
ex.:
if I set :
$x[0]=“value”;
$x[7]=“value”;
$x[9]=“value”;
$x[8]=“value”;
$x[1]=“value”;
$x[6]=“value”;
$x[5]=“value”;
$x[4]=“value”;
$x[3]=“value”;
$x[2]=“value”;
why doesnt it store in ORDER of its keys ( as opposed to order of creation)?
in other words when I var_dumped ($x)… i was expecting:
array(10) { [0]=> string(5) “value” [1]=> string(5) “value” [2]=> string(5) “value” [3]=> string(5) “value” [4]=> string(5) “value” [5]=> string(5) “value” [6]=> string(5) “value” [7]=> string(5) “value” [8]=> string(5) “value” [9]=> string(5) “value” }
instead of
array(10) { [0]=> string(5) “value” [7]=> string(5) “value” [9]=> string(5) “value” [8]=> string(5) “value” [1]=> string(5) “value” [6]=> string(5) “value” [5]=> string(5) “value” [4]=> string(5) “value” [3]=> string(5) “value” [2]=> string(5) “value” }
I know methods of correcting this. ( ksort, or just restoring the array using a fuction) but that would be an added step and I thought this kind of sorting would only have been needed for associative arrays,anyway. I would like to know if there is some concept I am missing here.
THANK U 
The concept is efficiency. Inserting in sorted order requires a search of the array on each insert, rather than following a single pointer to the end of the array. It may also require moving up to (n-1) elements of the array if the order is maintained by location in memory. For large arrays, either step can be unacceptably slow, and prevent you from solving whole classes of problems in PHP.
If you want a self-sorting array, then create that data structure.
Ok, that sort of makes sense.
I wanted to make sure it was anything in my code. I have no idea what my data is going to be or even in WHICH order it would be arranged (as usual, one is told to assume a client would ant anything at any order with one push of a button) I think a ksort() after all the data is entered would do this fine now.
THANKS!!
a php array is more of an ordered hash map or hash table. This makes it a swiss army knife because you can use it in so many different ways.
In a sense, it doesn’t even really have integer keys. You could say, integer keys are converted to string keys, for storage. For retrieval, strings keys that closely resemble integers are type cast back to int. Of course, php does keep track of the highest value “integer like string key” that it has encountered, for its auto next index generating functionality(example: $arr[] = ‘foo’;).
Malibu:
that’s intherestinf…
by what you are saying, I can infer that if I did this
$x[0]=1;
$x[1]=2;
$x[5]=3;
$x=5;
echo $x[6];
it would output “5”
right?
Yup. And this too.
$x[0]=1;
$x[1]=2;
$x[5]=3;
unset($x[5]);
$x[]=5; //even though index 5 is gone, 5 was still the highest, so next auto index is still 6
echo $x[6];
$x[3] = 'int';
$x['3'] = 'str';
echo $x[3]; // str, because 3 and '3' are same key. it got overwritten.
$x[3] = 'int';
$x[' 3'] = 'str';
$x['3.'] = 'str';
//all have different string values, so distinct
print_r($x);