SlideShare a Scribd company logo
Arrays in php
arrays
 Arrays in PHP is a type of data structure that allows us to store multiple elements of
similar data type under a single variable
 The arrays are helpful to create a list of elements of similar types, which can be
accessed using their index or key
 There are three different kind of arrays and each array value is accessed using an ID
which is called array index.
 Numeric array − An array with a numeric index. Values are stored and accessed in linear
fashion.
 Associative array − An array with strings as index. This stores element values in
association with key values rather than in a strict linear index order.
 Multidimensional array − An array containing one or more arrays and values are
accessed using 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.
 There are two ways to create indexed arrays:
 The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
 or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
Output:
I like Volvo, BMW and Toyota.
<?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 "<br>";
echo $name_one[2], "n";
echo $name_one[0], "n";
echo $name_one[4], "n";
echo "<br>";
// Second 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";
echo "<br>";
// Accessing the elements directly
echo "Accessing the 2nd array elements directly:n";
echo "<br>";
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
 The associative arrays are very similar to numeric arrays in term of
functionality but they are different in terms of their index.
 Associative array will have their index as string so that you can establish a
strong association between key and values.
<html>
<body>
<?php
/* First method to associate create array. */
$salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
/* Second method to create array. */
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
echo "Salary of qadir is ". $salaries['qadir']. "<br />";
echo "Salary of zara is ". $salaries['zara']. "<br />";
?>
</body>
</html>
Output:
Salary of mohammad is 2000
Salary of qadir is 1000
Salary of zara is 500
Salary of mohammad is high
Salary of qadir is medium
Salary of zara is low
Multidimensional Arrays
 Multi-dimensional arrays are such arrays that store another array at each
index instead of a single element.
 multi-dimensional arrays as an 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
 $cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
<html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
</body>
</html>
Output:
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
PHP foreach loop
 The foreach loop is used to traverse the array elements.
 It works only on array and object.
 The foreach loop works on elements basis rather than index.
 It provides an easiest way to iterate the elements of an array.
 Syntax
foreach ($array as $value) {
//code to be executed
}
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>
Output:
red
green
blue
yellow
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $val) {
echo "$x = $val<br>";
}
</body>
</html>
Output:
Peter = 35
Ben = 37
Joe = 43?>

More Related Content

PDF
PHP object calisthenics
PPT
PHP array 2
PPT
Arrays in php
PDF
Sorting arrays in PHP
PPTX
Array in php
PPTX
PHP Functions & Arrays
PDF
Arrays in PHP
PDF
PHP Unit 4 arrays
PHP object calisthenics
PHP array 2
Arrays in php
Sorting arrays in PHP
Array in php
PHP Functions & Arrays
Arrays in PHP
PHP Unit 4 arrays

What's hot (19)

PDF
Php array
PDF
PHP Arrays - indexed and associative array.
PPTX
How to Create an Array & types in PHP
PDF
Laravel collections an overview - Laravel SP
PDF
PHP 101
PPT
Php array
PPT
Class 4 - PHP Arrays
PPT
PDF
Web app development_php_04
PPTX
Introduction in php
PPT
Php Using Arrays
PDF
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
PPT
Smarty Template
PPT
Smarty Template
PPTX
Introduction in php part 2
PDF
Variables in PHP
PPT
Chapter 04 array
PPTX
Mysqlppt
PPTX
Object-Oriented Programming with PHP (part 1)
Php array
PHP Arrays - indexed and associative array.
How to Create an Array & types in PHP
Laravel collections an overview - Laravel SP
PHP 101
Php array
Class 4 - PHP Arrays
Web app development_php_04
Introduction in php
Php Using Arrays
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014
Smarty Template
Smarty Template
Introduction in php part 2
Variables in PHP
Chapter 04 array
Mysqlppt
Object-Oriented Programming with PHP (part 1)
Ad

Similar to Arrays in php (20)

PPTX
Chapter 2 wbp.pptx
PPTX
Unit 2-Arrays.pptx
PDF
Object Oriented Programming - 5.1. Array
PDF
Array String - Web Programming
PDF
PHP-Part3
PPTX
Web Application Development using PHP Chapter 4
PPTX
PHP Array Functions.pptx
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPT
Csharp4 arrays and_tuples
PDF
Java chapter 6 - Arrays -syntax and use
PPTX
CHAPTER 5 oop chapter 5 programming sem2
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
PPTX
Java script arrays
PPTX
Java script arrays
PPTX
3160713_WP_GTU_Study_Material_Presentations_Unit-5_17042022102823PM.pptx
PPTX
C# Array.pptx
PPTX
Arrays the beginners and intermediate.pptx
PDF
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
PPTX
Python array
PDF
DOC-20250225-WA0016..pptx_20250225_232439_0000.pdf
Chapter 2 wbp.pptx
Unit 2-Arrays.pptx
Object Oriented Programming - 5.1. Array
Array String - Web Programming
PHP-Part3
Web Application Development using PHP Chapter 4
PHP Array Functions.pptx
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Csharp4 arrays and_tuples
Java chapter 6 - Arrays -syntax and use
CHAPTER 5 oop chapter 5 programming sem2
Php my sql - functions - arrays - tutorial - programmerblog.net
Java script arrays
Java script arrays
3160713_WP_GTU_Study_Material_Presentations_Unit-5_17042022102823PM.pptx
C# Array.pptx
Arrays the beginners and intermediate.pptx
Difference between mysql_fetch_array and mysql_fetch_assoc in PHP
Python array
DOC-20250225-WA0016..pptx_20250225_232439_0000.pdf
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PDF
Basic Mud Logging Guide for educational purpose
PDF
Classroom Observation Tools for Teachers
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
PPH.pptx obstetrics and gynecology in nursing
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Computing-Curriculum for Schools in Ghana
Basic Mud Logging Guide for educational purpose
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Anesthesia in Laparoscopic Surgery in India
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

Arrays in php

  • 2. arrays  Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable  The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key  There are three different kind of arrays and each array value is accessed using an ID which is called array index.  Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.  Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.  Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices
  • 3. 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.  There are two ways to create indexed arrays:  The index can be assigned automatically (index always starts at 0), like this: $cars = array("Volvo", "BMW", "Toyota");  or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota";
  • 4. <html> <body> <?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?> </body> </html> Output: I like Volvo, BMW and Toyota.
  • 5. <?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 "<br>"; echo $name_one[2], "n"; echo $name_one[0], "n"; echo $name_one[4], "n"; echo "<br>"; // Second 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"; echo "<br>"; // Accessing the elements directly echo "Accessing the 2nd array elements directly:n"; echo "<br>"; 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
  • 6. Associative Arrays  The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index.  Associative array will have their index as string so that you can establish a strong association between key and values.
  • 7. <html> <body> <?php /* First method to associate create array. */ $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500); echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; /* Second method to create array. */ $salaries['mohammad'] = "high"; $salaries['qadir'] = "medium"; $salaries['zara'] = "low"; echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />"; echo "Salary of qadir is ". $salaries['qadir']. "<br />"; echo "Salary of zara is ". $salaries['zara']. "<br />"; ?> </body> </html> Output: Salary of mohammad is 2000 Salary of qadir is 1000 Salary of zara is 500 Salary of mohammad is high Salary of qadir is medium Salary of zara is low
  • 8. Multidimensional Arrays  Multi-dimensional arrays are such arrays that store another array at each index instead of a single element.  multi-dimensional arrays as an 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  $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) );
  • 9. <html> <body> <?php $cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>"; echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>"; echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>"; echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>"; ?> </body> </html> Output: Volvo: In stock: 22, sold: 18. BMW: In stock: 15, sold: 13. Saab: In stock: 5, sold: 2. Land Rover: In stock: 17, sold: 15.
  • 10. PHP foreach loop  The foreach loop is used to traverse the array elements.  It works only on array and object.  The foreach loop works on elements basis rather than index.  It provides an easiest way to iterate the elements of an array.  Syntax foreach ($array as $value) { //code to be executed }
  • 11. <html> <body> <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?> </body> </html> Output: red green blue yellow
  • 12. <html> <body> <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $val) { echo "$x = $val<br>"; } </body> </html> Output: Peter = 35 Ben = 37 Joe = 43?>