Unit - Ii (Array) ........
Unit - Ii (Array) ........
ARRAY
An array is a special variable that can hold many values under a single name, and you
can access the values by referring to an index number or name.
ANATOMY OF AN ARRAY :
In PHP, an array is a fundamental data structure that allows you to store multiple values in a
single variable.
Arrays can be indexed or associative, and their structure and handling are key to working
efficiently with data in PHP.
Here's a breakdown of the anatomy of an array in PHP.
Types of array
Modifying array element
Processing arrays with loops
Types of array :
In PHP, there are three types of arrays:
Indexed Arrays :
In indexed arrays each item has an index number. By default, the first item has
index 0, the second item has item 1, etc.
Syntax :
<html>
<body>
<pre>
<?php
?>
</pre>
</body>
</html>
Output :
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}
Example 2 : To access an array item you can refer to the index number.
<html>
<body>
<?php
echo $cars[0];
?>
</body>
</html>
Output :
Volvo
Associative arrays :
Associative arrays are arrays that use named keys that you assign to them.
To access an array item you can refer to the key name.
Syntax :
Example :
<html>
<body>
<pre>
<?php
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
echo $car[brand] . "<br>";
echo $car[model]. "<br>";
echo $car[year]. "<br>";
?>
</pre>
</body>
</html>
Output :
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
}
Ford
Mustang
1964
To update an existing array item, you can refer to the index number for indexed
arrays, and the key name for associative arrays.
Output :
Volvo
BMW
Toyota
After Modify:
Volvo
Ford
Toyota
<html>
<body>
<pre>
<?php
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
echo $cars[brand]."<br>";
echo $cars[model]."<br>";
echo $cars[year]."<br><br>";
echo $cars[brand]."<br>";
echo $cars[model]."<br>";
echo $cars[year]."<br>";
echo $cars[color]."<br>";
?>
</pre>
</body>
</html>
Output :
Ford
Mustang
1964
After Modify:
Ford
2024
Red
Arrays are fundamental data structures in PHP, and the ability to iterate through them
is crucial for manipulating and processing data.
Here , we see various approaches to iterate through arrays in PHP.
The PHP for loop is a basic way to iterate through an array. It is useful
when you need to access array elements by their index.
Syntax :
Example :
<?php
$length = count($arr);
?>
OUTPUT :
12345
Syntax :
If you need both the key and value during iteration, you can use this syntax:
<?php
$arr = [1, 2, 3, 4, 5];
foreach ($arr as $val) {
echo $val . ' ';
}
?>
Output :
1 2 3 4 5
<?php
$person = [
];
?>
Output :
name: John
age: 30
The PHP each() function can be used in combination with a while loop to
iterate through an array while maintaining the internal pointer.
Syntax :
Example :
<?php
$person = [
"name" => "John",
];
reset($person);
?>
Output :
name: John
age: 30
city: New York