Unit 2
Unit 2
By default, the first item has index 0, the second item has item 1, etc.
Example
Create and display an indexed array:
var_dump($cars);
Example
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
PHP supports multidimensional arrays that are two, three, four, five, or more
levels deep. However, arrays more than three levels deep are hard to
manage for most people.
Note: The implode() function accept its parameters in either order. However,
for consistency with explode(), you should use the documented order of
arguments.
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
Syntax
implode(separator,array)
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
Syntax
explode(separator,string,limit)
<!DOCTYPE html>
<html>
<body>
Syntax
array_flip(array)
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>
</body>
</html>
Output
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
This function uses array keys as variable names and values as variable
values. For each element it will create a variable in the current symbol table.
Syntax
extract(array, extract_rules, prefix)
<!DOCTYPE html>
<html>
<body>
<?php
$a = "Original";
extract($my_array);
?>
</body>
</html>
Arrays in PHP is a type of data structure that allows us to store multiple elements of
similar data type under a single variable thereby saving us the effort of creating a
different variable for every data. The arrays are helpful to create a list of elements of
similar types, which can be accessed using their index or key. Suppose we want to
store five names and print them accordingly. This can be easily done by the use of
five different string variables. But if instead of five, the number rises to a hundred,
then it would be really difficult for the user or developer to create so many different
variables. Here array comes into play and helps us to store every element within a
single variable and also allows easy access using an index or a key. An array is
created using an array() function in PHP.
There are basically three types of arrays in PHP:
Indexed or Numeric Arrays: An array with a numeric index where
values are stored linearly.
Associative Arrays: An array with a string index where instead of linear
storage, each value can be assigned a specific key.
Multidimensional Arrays: An array which contains single or multiple
array within it and can be accessed via multiple indices.
Indexed or Numeric Arrays
These type of arrays can be used to store any type of elements, but an index is
always a number. By default, the index starts at zero. These arrays can be created in
two different ways as shown in the following example:
<?php
// One way to create an indexed array
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
?>
Output:
Accessing the 1st array elements directly:
Ram
Zack
Raghav
Accessing the 2nd array elements directly:
RAM
ZACK
RAGHAV
Traversing: We can traverse an indexed array using loops in PHP. We can loop
through the indexed array in two ways. First by using for loop and secondly by using
foreach. You can refer to PHP | Loops for the syntax and basic use.
Example:
PHP
<?php
"Ram"=>"Rani", "Salim"=>"Sara",
"Raghav"=>"Ravina");
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";
?>
Output:
Looping using foreach:
Zack
Anthony
Ram
Salim
Raghav
PHP
<?php
$name_one = [
];
// Looping through an array using foreach
echo "Husband is " . $val . " and Wife is " . $val_value . "\n";
$keys = array_keys($name_one);
$round = count($name_one);
?>
Output:
Accessing the elements directly:
zara
sara
any
Rani
Ravina
Traversing Associative Arrays: We can traverse associative arrays in a similar way
did in numeric arrays using loops. We can loop through the associative array in two
ways. First by using for loop and secondly by using foreach. You can refer to PHP |
Loops for the syntax and basic use.
Example:
PHP
<?php
// Defining a multidimensional array
$favorites = array(
array(
),
array(
),
array(
);
// Accessing elements
?>
Output:
Looping using foreach:
Husband is Zack and Wife is Zara
Husband is Anthony and Wife is Any
Husband is Ram and Wife is Rani
Husband is Salim and Wife is Sara
Husband is Raghav and Wife is Ravina
PHP
<?php
// Defining a multidimensional array
$favorites = array(
"Dave Punk" => array(
"mob" => "5689741523",
"email" => "davepunk@gmail.com",
),
"Dave Punk" => array(
"mob" => "2584369721",
"email" => "montysmith@gmail.com",
),
"John Flinch" => array(
"mob" => "9875147536",
"email" => "johnflinch@gmail.com",
)
);
// Using for and foreach in nested form
$keys = array_keys($favorites);
for($i = 0; $i < count($favorites); $i++) {
echo $keys[$i] . "\n";
foreach($favorites[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "\n";
}
echo "\n";
}
?>
Output:
Dave Punk email-id is: davepunk@gmail.com
John Flinch mobile number is: 9875147536
Traversing Multidimensional Arrays: We can traverse through the multidimensional array
using 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:
Output:
Dave Punk
mob : 2584369721
email : montysmith@gmail.com
John Flinch
mob : 9875147536
email : johnflinch@gmail.com
PHP is a server-side scripting language designed specifically for web development. You can
learn PHP from the ground up by following this PHP Tutorial and PHP Examples.