Welcome! Log In Create A New Profile

Advanced

[PHP] difference between indexed and associative arrays?

Posted by Jeffry Killen 
Jeffry Killen
[PHP] difference between indexed and associative arrays?
April 05, 2017 12:00AM
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
I'm not sure I follow... Do you want array_diff but without relying on
keys? If so you could just wrap both arguments in an array_values call
could you not?

On Apr 4, 2017 15:55, "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
>
>
AshleySheridan
Re: [PHP] difference between indexed and associative arrays?
April 05, 2017 12:10AM
On Tue, 2017-04-04 at 14:54 -0700, Jeffry Killen 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
>

Have you looked at http://php.net/manual/en/function.array-diff-key.php
 ?

I don't understand fully what you want from your question, but
array_diff_key() looks like a good place to start.

-- 
Thanks,
Ash

http://www.ashleysheridan.co.uk
Kevin Waterson
Re: [PHP] difference between indexed and associative arrays?
April 05, 2017 12:20AM
<?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
>
>
> 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
Shawn McKenzie
Re: [PHP] difference between indexed and associative arrays?
April 05, 2017 03:00AM
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
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
As I understand, an associative array is exactly same as indexed array
but with an additional "property": the key.


El 04/04/17 a les 23:54, Jeffry Killen ha escrit:
> 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
On Wed, Apr 5, 2017 at 1:37 PM, Narcis Garcia <[email protected]> wrote:

> As I understand, an associative array is exactly same as indexed array
> but with an additional "property": the key.
>
>
> El 04/04/17 a les 23:54, Jeffry Killen ha escrit:
> > 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
>
>
​Indexed array has key property too, difference is that key are always
indexed (as name suggest).​

Also,

$a = [2 => 'foo', 4 => 'bar', 6 => 'baz'];
$a[] = 'foo1';
$a[] = 'bar1';
var_dump($a);

​Here keys(2 and 4) stays as is and keys(1 & 3) will be skipped.​ And next
key will largest key value + 1.

Output:

array(5) {
[2] =>
string(3) "foo"
[4] =>
string(3) "bar"
[6] =>
string(3) "baz"
[7] =>
string(4) "foo1"
[8] =>
string(4) "bar1"
}


--
Regards,
Jigar Dhulla
El 05/04/17 a les 10:31, Jigar Dhulla ha escrit:
> On Wed, Apr 5, 2017 at 1:37 PM, Narcis Garcia <[email protected]> wrote:
>
>> As I understand, an associative array is exactly same as indexed array
>> but with an additional "property": the key.
>>
>>
>> El 04/04/17 a les 23:54, Jeffry Killen ha escrit:
>>> 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
>>
>>
> ​Indexed array has key property too, difference is that key are always
> indexed (as name suggest).​
>
> Also,
>
> $a = [2 => 'foo', 4 => 'bar', 6 => 'baz'];
> $a[] = 'foo1';
> $a[] = 'bar1';
> var_dump($a);
>
> ​Here keys(2 and 4) stays as is and keys(1 & 3) will be skipped.​ And next
> key will largest key value + 1.
>
> Output:
>
> array(5) {
> [2] =>
> string(3) "foo"
> [4] =>
> string(3) "bar"
> [6] =>
> string(3) "baz"
> [7] =>
> string(4) "foo1"
> [8] =>
> string(4) "bar1"
> }
>
>

Then the logic is inverse:
"an indexed array is exactly same as associative array but with only
numeric keys"

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Garfield
Re: [PHP] difference between indexed and associative arrays?
April 05, 2017 04:30PM
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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
Re: [PHP] difference between indexed and associative arrays?
April 05, 2017 06:50PM
On 05.04.2017 at 16:23, Larry Garfield wrote:

> On 04/04/2017 06:23 PM, Jeffry Killen wrote:
>
>> 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.

Indeed. And if you need also the numeric index, you could do:

foreach ($arr as $i => $val) {
print "$i: $val\n";
}

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

I usually prefer foreach loops as well, but there is at least one case
where a for loop might be preferable, namely if there are a certain
number of runs and range() would be unnecessary overhead.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login