Can You Add Values to an Array without a Key?

deepak singh
Posts: 686
Joined: Fri Sep 30, 2016 4:19 pm

Can You Add Values to an Array without a Key?

Postby deepak singh » Sat Oct 01, 2016 5:23 pm

Can You Add Values to an Array without a Key?

rameshkhd
Posts: 1279
Joined: Sun Aug 28, 2016 10:45 am

Re: Can You Add Values to an Array without a Key?

Postby rameshkhd » Sat Oct 01, 2016 5:24 pm

Can You Add Values to an Array with a Key? The answer is yes and no. The answer is yes, because you can add values without specipickzyng any keys. The answer is no, because PHP will add a default integer key for you if you are not specipickzyng a key. PHP follows these rules to assign you the default keys:
• Assign 0 as the default key, if there is no integer key exists in the array.
• Assign the highest integer key plus 1 as the default key, if there are integer keys exist in the array.
Here is a PHP example script:
<?php
$mixed = array();
$mixed["Zero"] = "PHP";
$mixed[1] = "Perl";
$mixed["Two"] = "Java";
$mixed["3"] = "C+";
$mixed[""] = "Basic";
$mixed[] = "Pascal";
$mixed[] = "FORTRAN";
print("Array with default keys:\n");
print_r($mixed);
?>
This script will print:
Array with default keys:
Array
(
[Zero] => PHP
[1] => Perl
[Two] => Java
[3] => C+
[] => Basic
[4] => Pascal
[5] => FORTRAN
)


Return to “PHP”

Who is online

Users browsing this forum: No registered users and 3 guests