Re: [PHP] difference between indexed and associative arrays?

[email protected] (Larry Garfield)
Newsgroups php.general
Message-ID <[email protected]>
On 04/04/2017 06:23 PM, Jeffry Killen wrote:
>> On Apr 4, 2017, at 3:09 PM, Kevin Waterson <[email protected]> wrote:
>>
>> <?php
>>         $a1 = array( 'one'=>'koala', 'two'=>'dingo', 'three'=>'kangaroo',
>> 'four'=>'kookaburra', 'sixty'=>'wallaby' );
>>         $a2= array( 'koala', 'gecko', 'bilbi', 'crocodile', 'wallaby' );
>>
>>         $diff = array_diff( $a1, $a2 );
>>
>>         print_r( $diff );
>>
>>
>> On Wed, Apr 5, 2017 at 7:54 AM, Jeffry Killen <[email protected]> wrote:
>>
>>> Is there a function that will produce the difference between indexed and
>>> associative arrays, without actually testing for string or integer indices?
>>>
>>> (Although I realize that arrays can be mixed string and integer indeces.)
>>>
>>> Thank you for time and attention.
>>> JK
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
> To answer the replies,
>
> And associative array would be looped with foreach
> and an indexed array would be looped through with for and an iterator.

Untrue.  foreach() works just fine with both types of arrays.

Strictly speaking PHP doesn't have indexed arrays.  It has an ordered 
hash, which it calls an array, which has some special magic in it that 
gives it a monotonically increasing integer key if one is not specified, 
allowing it to behave kinda like an arbitrarily sized array.  There is 
no difference in the data structure itself.

$arr = ['A', 'B', 'C'];

foreach ($arr as $val) {
   print $val . PHP_EOL;
}

That's totally legit.  I honestly don't recall the last time I used a 
for() loop in PHP. :-)

--Larry Garfield
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.