8

As I know, in associative arrays, if the keys is not set, it will be set automatically. But it seem doesn't make sense in this case:

$a = array( '1' => 'One', '3', '2' => 'Two');
print_r($a);

Outputs:

Array ( [1] => One [2] => Two )

So where is the '3'?

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Jane
  • 173
  • 7
  • where did you get this?Associative array simply used for setting our own key than its default keys – PHP dev Jun 04 '15 at 06:11
  • I get this from [link](http://php.net/manual/en/function.array.php) . It says: "When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1". – Jane Jun 04 '15 at 06:16
  • kindly use this code and check it `$a = array( '1' => 'One', '3', '3' => 'Two'); print_r($a);` – PHP dev Jun 04 '15 at 06:22
  • the output will be `Array ( [1] => One [2] => 3 [3] => Two )` – PHP dev Jun 04 '15 at 06:22
  • Your works but I just want to know why my case don't in case i want to filter this case – Jane Jun 04 '15 at 06:28

3 Answers3

5

Within your user defined array you are assigning the keys manually your array means as

array(1 => 'One',3, 2 => 'Two');//[1] => One [2] => 3 [2] => Two

Here we have two identical index and as per DOCS its mentioned that the last overwrite the first

Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1.

Note that when two identical index are defined, the last overwrite the first.

In order to filter this case you can simply make some changes as

array(1 => 'One',2 =>'Two',3) // array ([1] => One [2] => Two [3] => 3)
array(3,1 => 'One',2 =>'Two') //array ([0] => 3 [1] => One [2] => Two)
array(1 => 'One',2 => 3 ,3 =>'Two')// array([1] => One [2] => 3 [3] => Two)

DOCS CHECK PARAMETERS

Community
  • 1
  • 1
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • Hey @Uchiha, one more question. How to remove the values that have no keys associated like above before processing the array? – Jane Jun 04 '15 at 06:57
  • Sorry Can't get you can you please show your expected output. – Narendrasingh Sisodia Jun 04 '15 at 06:59
  • I needing to get both keys and values: `foreach ( $a as $b => $c ) { printf( '%s = %s
    ', $b, $c); }` But not the keys that php automatically inserts in. So i want to clean array `$a` before `foreach` loop.
    – Jane Jun 04 '15 at 07:04
  • You'll get keys and values over here too. `foreach($a as $key => $value){ echo "{$key} => {$value}" ;}`. You can clean the array only if it won't contain values using `array_filter($array)` function – Narendrasingh Sisodia Jun 04 '15 at 07:06
  • I think you misunderstand my question. I mean getting the values that have keys have been set. E.g i want to remove '3' out of $a before processing $a. – Jane Jun 04 '15 at 07:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79619/discussion-between-uchiha-and-jane). – Narendrasingh Sisodia Jun 04 '15 at 07:17
  • [Check This](http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key) – Narendrasingh Sisodia Jun 04 '15 at 07:17
  • If you're going to take my answer and use it in your answer, at least make your answer a community wiki – viral Jun 05 '15 at 05:59
3

In php The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key

Like if :-

$a = array( 1 => 'One', 3, 3 => 'Two');
var_dump($a);

output will :-

array(3) {
  [1]=>
  string(3) "One"
  [2]=>
  int(3)
  [3]=>
  string(3) "Two"
}

Here for second value one is increment from previous value i.e 2.

Now

say array is :-

$a = array( '1' => 'One', '3', '3' => 'Two');
var_dump($a);

Output will

array(3) {
  [1]=>
  string(3) "One"
  [2]=>
  string(1) "3"
  [3]=>
  string(3) "Two"
}

Here also Here for second value one is increment from previous value i.e 2.

Now third case:-

If array is :-

$a = array( '1' => 'One', '1' => 'two' , '1' => 'Three');
var_dump($a);

Output will:-

array(1) {
  [1]=>
  string(5) "Three"
}

This is because associative array keep value as map and if key is present it overwrite value in this case 1 is overwrite 2 time as a result out is three

In your case :-

$a = array( '1' => 'One', '3', '2' => 'Two');
print_r($a);

Output is

Array
(
    [1] => One
    [2] => Two
)

this is because :-

first key map will:- '1' => 'one'

again

php will keep second value as '2' => '3'

Now as in array '2' is assigned as 'Two', value become

'2' => 'Two' which means it is overwriting.

Ashwani
  • 692
  • 2
  • 6
  • 16
0

@Uchiha is right, just as an include to that answer, if you want to avoid this problem, keep members of an array (which are not having keys specified) at the last


$a = array( '1' => 'One', '3', '2' => 'Two');

will dump

array (size=2)
  1 => string 'One' (length=3)
  2 => string 'Two' (length=3)


and keeping members with undefined keys at last
$a = array( '1' => 'One', '2' => 'Two', '3');

will dump

array (size=3)
  1 => string 'One' (length=3)
  2 => string 'Two' (length=3)
  3 => string '3' (length=1)


will avoid issues you are facing.
Community
  • 1
  • 1
viral
  • 3,724
  • 1
  • 18
  • 32