Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are numeric and associative arrays in PHP two different things?

Tags:

arrays

php

This is a deeper dive into a previous question I had here: Can items in PHP associative arrays not be accessed numerically (i.e. by index)?

According to W3Schools, :

In PHP, there are three kind of arrays:

  • Numeric array - An array with a numeric index
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

But is this accurate? Each element in the array can be assigned either an index or a string as a key- so what happens when the two are mixed in the same array?

$myArray[0] = 'value1';
$myArray['one'] = 'value2';
like image 525
Yarin Avatar asked May 08 '11 23:05

Yarin


People also ask

What is difference between numeric and associative array in PHP?

Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion. Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

What are the two types of PHP arrays and how do they differ?

Create an Array in PHP In PHP, there are three types of arrays: Indexed arrays - Arrays with a numeric index. Associative arrays - Arrays with named keys. Multidimensional arrays - Arrays containing one or more arrays.

Can arrays in PHP have different data types?

Not going to put oil on the fire of the PHP Arrays are no arrays here… But yes, you can put different variable types (string, int, …) together in a PHP thing called Array.


3 Answers

All arrays in PHP are the same; they're implemented as hash maps which associate keys to values, whatever type the keys may be.

Manual:

The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.

If an array had both numeric and non-numeric indices, though, I'd still call it an associative array. The meaning of "associative" still stands.

Wikipedia:

An associative array is an abstract data type composed of a collection of unique keys and a collection of values, where each key is associated with one value (or set of values).

...

From the perspective of a computer programmer, an associative array can be viewed as a generalization of an array. While a regular array maps an integer key (index) to a value of arbitrary data type, an associative array's keys can also be arbitrarily typed. In some programming languages, such as Python, the keys of an associative array do not even need to be of the same type.

For the last sentence, the same applies for PHP, as shown in your example.

like image 59
BoltClock Avatar answered Sep 28 '22 02:09

BoltClock


PHP doesn't really have arrays. They are dictionaries. Numeric keys are allowed at the same time as string keys. They can be mixed and do coexist.

(Actually string keys like "123" are always treated as integers. PHP does not keep the type information for them.)

If you want a different behaviour you could implement and extend ArrayObject however. And it would be possible to implement a map, where numeric keys functioned as alias to string indexes.

like image 24
mario Avatar answered Sep 28 '22 02:09

mario


In general, you should read the official documentation rather than W3Schools.

An array can contain whatever members it wants with whatever keys it wants.

The description provided by W3Schools is quite ambiguous, or even wrong.

  • Numeric array - An array with a numeric index

I'd say a numeric array is an array with only integer indexes. An array with one I'd probably call a mixed (or associative, see below) array, if I had to call it anything.

  • Associative array - An array where each ID key is associated with a value.

I don't know about that description. I'd say an array can be associative if it maps strings to values instead of numerical indexes.

  • Multidimensional array - An array containing one or more arraysNumeric array - An array with a numeric index

An associative array can contain arrays too, which makes it multidimensional.

Keep in mind that an array with all numeric keys (even if in a string) will always be treated as a numeric array. This can mean different things in different contexts.

$arr = array(
 '1' => 'abc',
 2 => 'def'
);

var_dump($arr);

Output

array(2) {
  [1]=>
  string(3) "abc"
  [2]=>
  string(3) "def"
}
like image 45
alex Avatar answered Sep 28 '22 01:09

alex