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

[email protected] (Shawn McKenzie)
Newsgroups php.general
Message-ID <CABPrARieWOs1GQNJkKmc9wOUAzTApSXmhyQgtM3Uvym0oU1d6Q@mail.gmail.com>
No there isn't and it doesn't much matter.  You can use foreach() on
both and if associative you can use array_values() to get an integer
indexed array if for whatever reason you want to use a for() loop.

On Tue, Apr 4, 2017 at 6:23 PM, Jeffry Killen <[email protected]> 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.
>
> from examples above:
> $a1 is an associative array so it would be looped through with foreach
> and
> $a2 is an indexed array so it would be looped through with a for loop
> with an integer iterator.
>
> So, I want to know before hand what type of loop to use on an unanticipated
> array type.
>
> This example I am assuming could be valid, where $a3[0] == 'one';
> and $a3[3] == 'four':
> $a3 = array('one', 'two'=>'two', 'three'=>'three', 'four');
>
> Thanks again for time and attention
> JK
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
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.