Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

PHP Array

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

PHP Array

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

PHP ARRAYS

Arrays in PHP are a type of data structure that allows us


to store multiple elements of similar or different data
types 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.
An array is created using an array() function in PHP.
Types of Array in PHP

There are mainly three types of array in php


1. Indexed or Numeric Arrays: An array with a numeric index
where values are stored linearly.
• PHP index is represented by number which starts from 0.
• We can store number, string and object in the PHP array.
• All PHP array elements are assigned to an index number by default.
• There are two ways to define indexed array:
Examples of Indexed or Numeric Arrays

<?php

// One way to create an indexed array


$name_one = array("Zack", "Anthony", "Ram", "Salim",
"Raghav");

// Accessing the elements directly


echo "Accessing the 1st array elements directly:\n";
echo $name_one[2], "\n";
echo $name_one[0], "\n";
echo $name_one[4], "\n";

// Second way to create an indexed array


$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
// Accessing the elements directly
echo "Accessing the 2nd array elements
directly:\n";
echo $name_two[2], "\n";
echo $name_two[0], "\n";
echo $name_two[4], "\n";

?>
OUTPUT
Accessing the 1st array elements directly:
Ram
Zack
Raghav

Accessing the 2nd array elements directly:


RAM
ZACK
RAGHAV
Associative Arrays
• These types of arrays are similar to the indexed arrays but instead of linear
storage, every value can be assigned with a user-defined key of string type.
• We can associate name with each array elements in PHP using => symbol.
• Example:
• <?php

• // One way to create an associative array
• $name_one = array("Zack"=>"Zara", "Anthony"=>"Any",
• "Ram"=>"Rani", "Salim"=>"Sara",
• "Raghav"=>"Ravina");
// Second way to create an associative
array
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara"; O/P
$name_two["raghav"] = "ravina"; Accessing the elements
// Accessing the elements directly
directly:
echo "Accessing the elements directly:\ zara
n"; sara
echo $name_two["zack"], "\n"; any
echo $name_two["salim"], "\n"; Rani
echo $name_two["anthony"], "\n"; Ravina
echo $name_one["Ram"], "\n";
echo $name_one["Raghav"], "\n";
Multidimensional Arrays
• PHP multidimensional array is also known as array of arrays.
• As the name suggests, every element in this array can be an array and they
can also hold other sub-arrays within.
• Arrays or sub-arrays in multidimensional arrays can be accessed using
multiple dimensions.
• It allows you to store tabular data in an array.
• PHP multidimensional array can be represented in the form of matrix which
is represented by row * column.
Example

<?php

// Defining a multidimensional array


$favorites = array(
array(
"name" => "Dave Punk",
"mob" => "5689741523",
"email" => "davepunk@gmail.com",
),
array(
"name" => "Monty Smith",
"mob" => "2584369721",
"email" => "montysmith@gmail.com",
),
array(
"name" => "John Flinch",
"mob" => "9875147536",
"email" => "johnflinch@gmail.com",
// Accessing elements
echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n";
echo "John Flinch mobile number is: " . $favorites[2]
["mob"];

?>

O/P
Dave Punk email-id is: davepunk@gmail.com
John Flinch mobile number is: 9875147536

You might also like