0% found this document useful (0 votes)
22 views8 pages

Unit - Ii (Array) ........

Uploaded by

theraja1311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Unit - Ii (Array) ........

Uploaded by

theraja1311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT – II

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 - Arrays with a numeric index


 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing one or more 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 :

array(value1, value2, value3, etc)

Example 1: Create and display an indexed array

<html>

<body>

<pre>

<?php

$cars = array("Volvo", "BMW", "Toyota");


var_dump($cars);

echo “Car Name Is :” .$cars ;

?>

</pre>

</body>

</html>

Output :

array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
}

Car Name Is : Volvo, BMW, Toyota

Example 2 : To access an array item you can refer to the index number.

<html>

<body>

<?php

$cars = array("Volvo", "BMW", "Toyota");

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 :

array(key => value,key2 => value2,key3 => value3,...)

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

MODIFYING ARRAY ELEMENT :

 To update an existing array item, you can refer to the index number for indexed
arrays, and the key name for associative arrays.

Example 1 : Index Number


<html>
<body>
<pre>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[0]."<br>";
echo $cars[1]."<br>";
echo $cars[2]."<br>";
$cars[1] = "Ford";
echo "After Modify: <br>";
echo $cars[0]."<br>";
echo $cars[1]."<br>";
echo $cars[2]."<br>";
?>
</pre>
</body>
</html>

Output :

Volvo
BMW
Toyota
After Modify:
Volvo
Ford
Toyota

Example 2 : Key Name

<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>";

$cars["color"] = "Red"; //ADD ARRAY


$cars["year"] = 2024; //UPDATE ARRAY

unset($cars["model"]); //REMOVE ARRAY

echo "After Modify: <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

PROCESSING ARRAYS WITH LOOPS :

 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.

 Using For Loop


 Using Foreach Loop
 Using While Loop
Using For Loop :

 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 :

for (initialization; condition; increment/decrement)

Example :

<?php

$arr = [1, 2, 3, 4, 5];

$length = count($arr);

for ($i = 0; $i < $length; $i++) {

echo $arr[$i] . ' ' ;


}

?>

OUTPUT :

12345

Using Foreach Loop :

 The PHP foreach loop is an method to iterate through an array. There is no


need of index number in foreach loop.

Syntax :

foreach ($array as $value)

 If you need both the key and value during iteration, you can use this syntax:

foreach ($array as $key => $value)

Example 1: Iterating over an array of values

<?php
$arr = [1, 2, 3, 4, 5];
foreach ($arr as $val) {
echo $val . ' ';
}
?>
Output :

1 2 3 4 5

Example 2 : Iterating over an associative array

<?php

$person = [

"name" => "John",

"age" => 30,

"city" => "New York"

];

foreach ($person as $key => $value) {

echo $key . ": " . $value . "\n";

?>

Output :

name: John

age: 30

city: New York

Using While Loop :

 The PHP each() function can be used in combination with a while loop to
iterate through an array while maintaining the internal pointer.

Syntax :

while ($element = each($person))

Example :

<?php

$person = [
"name" => "John",

"age" => 30,

"city" => "New York"

];

reset($person);

while ($element = each($person)) {

echo $element['key'] . ": " . $element['value'] . "\n";

?>

Output :

name: John
age: 30
city: New York

You might also like