Arrays
An arrayin PHP is a collection of key/value pairs.
This means that it maps keys or indexes to values.
Array indexescan be either integers or strings
whereas values can be of any type.
Arrays in PHP are implemented using hash tables, which
means that accessing a value has an average complexity.
array()construct Arrays can be declared using the array()language
construct, which generally takes the following form elements inside
square brackets, [], are optional:
array([key =>] value, [key =>] value, ...)
The key is optional, and when it’s not specified, the key is
automatically assigned one more than the largest previous
integer key starting with 0. You can intermix the use with
and without the key even within the same declaration.
The value itself can be of any PHP type, including an array.
Arrays containing arrays give a similar result as multi-dimensional
arrays in other languages.
Accessing Array Elements Array elements can be accessed by using
the $arr[key]notation, where keyis either an integer or string expression.
When using a constant string for key,make sure you don’t forget the single or
double quotes, such as $arr["key"]. This notation can be used for both reading
array elements and modifying or creating new elements.
$arr1 = array(1, 2, 3);
$arr2[0] = 1;
$arr2[1] = 2;
$arr2[2] = 3;
print_r($arr1);
print_r($arr2);
The print_r()function has not been covered yet in this book, but when it
is passed an array, it prints out the array’s contents in a readable way. You can
use this function when debugging your scripts.
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
So, you can see that you can use both the array()construct
and the $arr[key] notation to create arrays.
Usually, array()is used to declare arrays
whose elements are known at compile-time, and
the $arr[key]notation is used when the elements
are only computed at runtime.
PHP also supports a special notation, $arr[], where the key is
not specified. When creating new array offsets using
this notation , the key is automatically assigned as one
more than the largest previous integer key.
Therefore, the previous example can be rewritten as follows:
$arr1 = array(1, 2, 3);
$arr2[] = 1;
$arr2[] = 2;
$arr2[] = 3;
$arr1 = array("name" => "John", "age" => 28);
$arr2["name"] = "John";
$arr2["age"] = 28;
if ($arr1 == $arr2) {
print '$arr1 and $arr2 are the same' . "\n";
}
Reading array values You can use the
$arr[key]notation to read array values.
The next few examples build on top of the previous example:
print $arr2["name"];
if ($arr2["age"] < 35) {
print " is quite young\n";
}
Php Interview-questions-with-answers
Php code for valid number
Php file uploading code
Php by code
Advanced php
Php global variables
Top 550 php interview-questions
An arrayin PHP is a collection of key/value pairs.
This means that it maps keys or indexes to values.
Array indexescan be either integers or strings
whereas values can be of any type.
Arrays in PHP are implemented using hash tables, which
means that accessing a value has an average complexity.
array()construct Arrays can be declared using the array()language
construct, which generally takes the following form elements inside
square brackets, [], are optional:
array([key =>] value, [key =>] value, ...)
The key is optional, and when it’s not specified, the key is
automatically assigned one more than the largest previous
integer key starting with 0. You can intermix the use with
and without the key even within the same declaration.
The value itself can be of any PHP type, including an array.
Arrays containing arrays give a similar result as multi-dimensional
arrays in other languages.
Accessing Array Elements Array elements can be accessed by using
the $arr[key]notation, where keyis either an integer or string expression.
When using a constant string for key,make sure you don’t forget the single or
double quotes, such as $arr["key"]. This notation can be used for both reading
array elements and modifying or creating new elements.
$arr1 = array(1, 2, 3);
$arr2[0] = 1;
$arr2[1] = 2;
$arr2[2] = 3;
print_r($arr1);
print_r($arr2);
The print_r()function has not been covered yet in this book, but when it
is passed an array, it prints out the array’s contents in a readable way. You can
use this function when debugging your scripts.
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
So, you can see that you can use both the array()construct
and the $arr[key] notation to create arrays.
Usually, array()is used to declare arrays
whose elements are known at compile-time, and
the $arr[key]notation is used when the elements
are only computed at runtime.
PHP also supports a special notation, $arr[], where the key is
not specified. When creating new array offsets using
this notation , the key is automatically assigned as one
more than the largest previous integer key.
Therefore, the previous example can be rewritten as follows:
$arr1 = array(1, 2, 3);
$arr2[] = 1;
$arr2[] = 2;
$arr2[] = 3;
$arr1 = array("name" => "John", "age" => 28);
$arr2["name"] = "John";
$arr2["age"] = 28;
if ($arr1 == $arr2) {
print '$arr1 and $arr2 are the same' . "\n";
}
Reading array values You can use the
$arr[key]notation to read array values.
The next few examples build on top of the previous example:
print $arr2["name"];
if ($arr2["age"] < 35) {
print " is quite young\n";
}
Php Interview-questions-with-answers
Php code for valid number
Php file uploading code
Php by code
Advanced php
Php global variables
Top 550 php interview-questions