php arrays. outline o what is array in php ? o numeric array o associative array o multidimensional...
TRANSCRIPT
![Page 1: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/1.jpg)
PHPPHP
Arrays
![Page 2: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/2.jpg)
Outline
o What is array in PHP ?
o Numeric array
o Associative array
o Multidimensional array
![Page 3: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/3.jpg)
What is array in PHP ?
A variable is a storage area holding a number or text.
The problem is, a variable will hold only one value.
An array is a special variable, which can store
multiple values in one single variable.
![Page 4: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/4.jpg)
What is array in PHP ?
However, what if you want to loop through the cars and find
a specific one? And what if you had not 3 cars, but 300?
The best solution here is to use an array!
An array can hold all your variable values under a single
name. And you can access the values by referring to the array
name.
Each element in the array has its own index so that it can be
easily accessed.
![Page 5: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/5.jpg)
Kind of arrays in PHP
Numeric array - An array with a numeric index
Associative array - An array where each ID key is
associated with a value
Multidimensional array - An array containing one or
more arrays
![Page 6: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/6.jpg)
Numeric Arrays
• A numeric array stores each array element
with a numeric index.
• There are two methods to create a numeric
array:
![Page 7: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/7.jpg)
Numeric Arrays1. In the following example the index are automatically assigned (the index starts at 0):
- $cars = array ("Saab","Volvo","BMW","Toyota");
2. The following example we assign the index manually:
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
$array_name = array(val1, val2, val3, val4,……);
![Page 8: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/8.jpg)
Numeric Arrays-example<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>
![Page 9: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/9.jpg)
Numeric Arrays-example<?php
$array = array(1, 2,3,4,5,6,7,8,9);
for ($i = 0; $i < 9; $i++)
{
echo “Number : “. $array[$i];
}
?>
![Page 10: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/10.jpg)
Print_r()
• Print construction of array as group of locations and the value stored in it
Print_r($array );
![Page 11: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/11.jpg)
Print_r()<?php
$arr = array(1,2,3,4,5);
Print_r($arr);
?>
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
![Page 12: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/12.jpg)
Count()• this is build in function , that count number of value store in array or number of character in string .
Count($variable );
![Page 13: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/13.jpg)
Numeric Arrays-example<?php
$array = array(“M”, ”u”,”n”,”e”,”e”,”r”);
for ($i = 0; $i < count($array); $i++)
{
echo $array[$i];
}
?>
![Page 14: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/14.jpg)
foreach
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
foreach ($array as $value) { code to be executed; }
![Page 15: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/15.jpg)
Numeric Arrays-example<?php
$colors = array('red', 'blue', 'green', 'yellow');
foreach ($colors as $color) {
echo "Do you like $color?\n";
}
?>
![Page 16: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/16.jpg)
Find in Array
- This function return true if the $search found in $orginal_data , otherwise return false.
in_array($search , $orginal_data)
![Page 17: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/17.jpg)
Find in Array
<?php
$os = array("Mac", “W7", “W8", "Linux");
if (in_array(“XP", $os)) {
echo "Got XP";
}
if (in_array(“Mac", $os)) {
echo "Got Mac";
}
?>
![Page 18: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/18.jpg)
Associative Arrays
• An associative array, each ID key is associated with a value.
• When storing data about specific named values, a numerical array is not always the best way to do it.
• With associative arrays we can use the values as keys and assign values to them.
![Page 19: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/19.jpg)
Associative Arrays
• $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
or• $ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
$array_name= array($index1=>$val1, $index1=>$val1,…..);
![Page 20: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/20.jpg)
Associative Arrays <?php
$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // barecho $arr[12]; // 1?>
![Page 21: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/21.jpg)
Associative Arrays - example<?php
// This array is the same as ...
array(5 => 43, 32, 56, "b" => 12);
// ...this array
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
![Page 22: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/22.jpg)
Associative Arrays - example<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
![Page 23: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/23.jpg)
Associative Arrays - example<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script
$arr["x"] = 42; // This adds a new element to
// the array with key "x"
unset($arr[5]); // This removes the element from the array
unset($arr); // This deletes the whole array
?>
![Page 24: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/24.jpg)
Associative Arrays - example<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
![Page 25: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/25.jpg)
Multidimensional Arrays
• In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array,
$myArray = array( array( value1, value2, value3 ), array( value4, value5, value6 ), array( value7,
value8, value9 ) );
![Page 26: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/26.jpg)
Multidimensional Arrays<?php
$fruits = array ( "fruits" => array ( "a" => "orange", "b" => "banana“,"c" => "apple”),"numbers" => array ( 1, 2,3,4,5,6), "holes" => array ("first“,5 => "second“,"third”));
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
?>
![Page 27: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/27.jpg)
Multidimensional Arrays
<?php
$families = array(“capital"=>array(“A“,“B“,“C”),
“small"=>array(“a”, “b”, “c”),
“number"=>array(“1“,“2“,”3”));
foreach($families as $value)
{foreach($value as $values)
{echo $values.” ”;}
Echo “<br/>”}
?>
![Page 28: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/28.jpg)
Multidimensional Arrays<?php
$try[] = array("11", "12", "15", "22", "41", "42");
$try[] = array("6", "7", "16", "17", "22", "23");
$count = count ($try);
for ($i=0; $i<$count; $i++)
{
$countmore=count($try[0]);
for ($j=0; $j < $countmore; $j++)
{
print ("i$i j$j " . $try[$i][$j] . "<br> ");
}
print ("<br>");
}
?>
![Page 29: PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649e615503460f94b5d4c1/html5/thumbnails/29.jpg)
Multidimensional Arrays<?php
$try[] = array("11", "12", "15", "22", "41", "42");
$try[] = array("6", "7", "16", "17", "22", "23"); $count = count ($try); for ($i=0; $i<$count; $i++)
{
$countmore=count($try[0]);
for ($j=0; $j < $countmore; $j++)
{ print ("i$i j$j " . $try[$i][$j] . "<br> "); }
print ("<br>");
}
?>