On Sat, Sep 1, 2012 at 4:52 AM, Lester Caine <les...@lsces.co.uk> wrote:
> Sherif Ramadan wrote:
>>
>> As it stands today, that code would result in the following:
>>
>> $numbers = array();
>> $numbers[-1] = 5;
>> $numbers[] = 6;
>> var_dump($numbers);
>> /*
>> array(2) {
>>    [-1]=>
>>    int(5)
>>    [0]=>
>>    int(6)
>> }
>> */
>
>
> I think that it is just clarifying this further by adding
> $numbers = array(0,1,2,3,4);
> Which gives
> array(7) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4)
> [-1]=> int(5) [5]=> int(6) }

I have no idea how it would. I think you supplied the wrong code here.


> and
> foreach( $numbers as $value ) { print($value); } gives 0123456 as I would
> have expected!
> AND print( $numbers[-1] ); still returns '5'
>
> I can NOW use ksort() to really move the '5' to the top of the list.
> array(7) { [-1]=> int(5) [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3)
> [4]=> int(4) [5]=> int(6) }
>
> Bottom line ... perhaps we need a DIFFERENT means of identifying position in
> the array since using -ve keys IS a valid situation in real life. Personally
> I use -ve positions in the database tables to save having to process all
> records when adding items at the top, so the associative array quite
> correctly has -ve key values. I would not want PHP screwing around with that
> when reading the array in :)

I can't understand what you mean by "Different means of identifying
position in the array"? If you mean a way to access an array's element
by its position in the array then yes, we already have that. It's
called array_slice() see http://php.net/array-slice which allows you
to access elements in the array by their offset and that includes
using a negative offset. The key and the offset are two completely
different things.


>
> I think I am right in saying that 'real array' always start at 0 or 1, and
> positioning is explicit. So for simple stings then ne6ative indexes make
> perfect sense. But for PHP 'container arrays' we can legally have -ve keys,
> and removing that is simply not acceptable.
>

Yes, the position is always explicit, the key isn't ever responsible
for determining the position of an element in the array. The value of
the PHP key has never been responsible for determining the position of
the element in the array.

$array[99999] = 'first element';
$array[0] = 'second element';
var_dump($array);
/*
array(2) {
  [99999]=>
  string(13) "first element"
  [0]=>
  string(14) "second element"
}
*/

$array[0] = 'first element';
$array[99999] = 'second element';
var_dump($array);
/*
array(2) {
  [99999]=>
  string(14) "second element"
  [0]=>
  string(13) "first element"
}
*/

The order has always been determined internally so the values of these
keys mean nothing in terms of order as you can see.


> --
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk
> Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
>
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to