Example
create and access numeric arrays:-
used array() function to create array.
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
/* Second method to create array. */
$numbers[0] = "one";
$numbers[1] = "two";
$numbers[2] = "three";
$numbers[3] = "four";
$numbers[4] = "five";
foreach( $numbers as $value ) {
echo "Value is $value <br />";
}
?>
</body>
</html>
output −
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is one
Value is two
Value is three
Value is four
Value is five
Associative Arrays
The associative arrays are very similar to numeric arrays in functionality but they are different in terms of their index.
Associative array will have their index as a string to establish a strong association between key and values.
Syntax:-
$variable = array("key" => value1, " key " => value2, " key " => value3);
Example:- To store the salaries of employees in an array, use the employees' names as the keys in an associative array, and the value in their respective salaries.
NOTE − Don't keep associative array inside double quote while printing otherwise it would not return any value.
Traversing Associative Arrays: Traverse associative arrays use a similar way as numeric arrays using loops.
Example
<html>
<body>
<?php
/* First method to associate create array. */
$salaries = array("mohan" => 2000, "rama" => 1000, "ramesh" => 500);
echo "Salary of mohan is ". $salaries[‘mohan’] . "<br />";
echo "Salary of rama is ". $salaries[' rama ']. "<br />";
echo "Salary of ramesh is ". $salaries[' ramesh ']. "<br />";
/* Second method to create array. */
$salaries[' mohan '] = "high";
$salaries[' rama '] = "medium";
$salaries[' ramesh '] = "low";
echo "Salary of mohan is ". $salaries[' mohan '] . "<br />";
echo "Salary of rama is ". $salaries[' rama ']. "<br />";
echo "Salary of ramesh is ". $salaries[' ramesh ']. "<br />";
?>
</body>
</html>
output −
Salary of mohan is 2000
Salary of rama is 1000
Salary of ramesh is 500
Salary of mohan is high
Salary of rama is medium
Salary of ramesh is low
Multidimensional Arrays
In a multi-dimensional array, each element in the main array can also be an array and each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple indexes.
Syntax:-
$variable = array(
"key" => array ( " key " => value, " key " => value, " key " => value),
" key " => array ( " key " => value, " key " => value, " key " => value),
" key " => array ( " key " => value, " key " => value, " key " => value) );
Traversing Multidimensional Arrays: Traverse in the multidimensional array by for and foreach loop in a nested way. That is, one for loop for the outer array and one for loop for the inner array.
Example
create a two-dimensional array to store marks of three students in three subjects −
<html>
<body>
<?php
$marks = array(
"mohammad" => array (
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"qadir" => array (
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"zara" => array (
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
/* Accessing multi-dimensional array values */
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for qadir in maths : ";
echo $marks['qadir']['maths'] . "<br />";
echo "Marks for zara in chemistry : " ;
echo $marks['zara']['chemistry'] . "<br />";
?>
</body>
</html>
output−
Marks for mohammad in physics : 35
Marks for qadir in maths : 32
Marks for zara in chemistry : 39
The count() the function is used to return the length (the number of elements) of an array:
Ex.
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Note:- The var_dump() function is used to show information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
100 => -100,
-100 => 100,
);
var_dump($array);
?>
Output:
array(4) {
["foo"]=>
string(3) "bar"
["bar"]=>
string(3) "foo"
[100]=>
int(-100)
[-100]=>
int(100)
}
Accessing array elements with the square bracket syntax
Array elements can be accessed using the array[key] syntax.
Example # Accessing array elements
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
Output:
string(3) "bar"
int(24)
string(3) "foo"
Creating/modifying with the square bracket syntax
An existing array can be modified by explicitly setting values in it.
This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([ ]).
$arr[key] = value;
$arr[ ] = value;
To change a certain value, assign a new value to that element using its key. To remove a key/value pair, call the unset() function on it.
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script
$arr["x"] = 42; // This adds a new element to
// the array with key "x"
unset($arr[5]); // This removes the element from the array
unset($arr); // This deletes the whole array
?>
Example:-
<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);
// Now delete every item, but leave the array itself intact:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);
// Append an item (note that the new key is 5, instead of 0).
$array[] = 6;
print_r($array);
// Re-index:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)
============================================
0 Comments