ARRAYS The different types of arrays in PHP




![int temperature[2] = { 40, 42, 44 } ; C/C++-LANGUAGE int temperature[ ] = int temperature[2] = { 40, 42, 44 } ; C/C++-LANGUAGE int temperature[ ] =](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-5.jpg)
![EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-6.jpg)
![EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-7.jpg)



![EXAMPLE 3: array 3. php <? php $a=array(); $a[0]=10; $a[1]=20; a [0] [1] [2] EXAMPLE 3: array 3. php <? php $a=array(); $a[0]=10; $a[1]=20; a [0] [1] [2]](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-11.jpg)


![EXAMPLE 1 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole"=>44); print $temperature["guntur"]; print $temperature[“vijayawada "]; print EXAMPLE 1 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole"=>44); print $temperature["guntur"]; print $temperature[“vijayawada "]; print](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-14.jpg)
![EXAMPLE 2 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole "=>44); foreach($temperature as $k=>$v) print $temperature[$k]; EXAMPLE 2 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole "=>44); foreach($temperature as $k=>$v) print $temperature[$k];](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-15.jpg)


![EXAMPLE 2: <? php $a=array( “ravi”, “raj” ) ) ); echo $a[0][0][0]; ? > EXAMPLE 2: <? php $a=array( “ravi”, “raj” ) ) ); echo $a[0][0][0]; ? >](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-18.jpg)









- Slides: 27

ARRAYS

ü The different types of arrays in PHP are: i. Numerical Arrays. ii. Associative Arrays. iii. Multi Dimensional Arrays. ü Let us discuss types of arrays in detail.

NUMERICAL ARRAYS

DEFINITION: Numerical arrays are arrays that have integer values as their indices. EXAMPLE 1: Store all the temperatures recorded at different places on a particular day in Andhra Pradesh.
![int temperature2 40 42 44 CCLANGUAGE int temperature int temperature[2] = { 40, 42, 44 } ; C/C++-LANGUAGE int temperature[ ] =](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-5.jpg)
int temperature[2] = { 40, 42, 44 } ; C/C++-LANGUAGE int temperature[ ] = { 40, 42, 44 } ; JAVA- $temperature = array(40, 42, 44); LANGUAGE PHP REPRESENTATION OF ARRAYS IN C, C++, JAVA, PHP temperature 40 42 44 [0] [1] [2] Numerical Indices
![EXAMPLE 2 char names 4 ravi raju ramu CCLANGUAGE String EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-6.jpg)
EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String names[ ] = { "ravi", "raju", "ramu“ }; JAVA- $names = array(“ravi”, ”raju”, ”ramu” ); PHP LANGUAGE REPRESENTATION OF ARRAYS IN C, C++ [1] [2] [0] names r a v i [0] [1] [2] [3] Numerical Indices r a m u [0] [1] [2] [3] r a j u [0] [1] [2] [3] Numerical Indices
![EXAMPLE 2 char names 4 ravi raju ramu CCLANGUAGE String EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-7.jpg)
EXAMPLE 2: char names[ ][4] = { “ravi”, ”raju”, ”ramu” } ; C/C++-LANGUAGE String names[ ] = { "ravi", "raju", "ramu“ }; JAVA- $names = array(“ravi”, ”raju”, ”ramu” ); PHP LANGUAGE REPRESENTATION OF ARRAYS IN JAVA, PHP names ravi [0] raju ramu [1] [2] Numerical Indices

ü The different ways of defining numerical arrays in PHP are: (i) $a=array( ); // Creating an Empty Array ARRAY NAME (ii) $a=array(10, 20, 30, 40, 50); // Creating an array with some elements ARRAY NAME (iii) $a=array(“ashok”, ”rajiv”, ”rupesh”); // Creating an Array with some elements ARRAY NAME

EXAMPLE 1: array 1. php <? php $a=array(10, 20, 30, 40, 50); for($i=0 ; $i<count($a) ; $i++) print $a[$i]; ? > OUTPUT: 10 20 30 40 50 a 10 20 30 40 50 [0] [1] [2] [3] [4]

EXAMPLE 2: array 2. php <? php $a=array(10, 20, 30, 40, 50); foreach($a as $i) print $i; ? > OUTPUT: 10 20 30 40 50 a 10 20 30 40 50 [0] [1] [2] [3] [4]
![EXAMPLE 3 array 3 php php aarray a010 a120 a 0 1 2 EXAMPLE 3: array 3. php <? php $a=array(); $a[0]=10; $a[1]=20; a [0] [1] [2]](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-11.jpg)
EXAMPLE 3: array 3. php <? php $a=array(); $a[0]=10; $a[1]=20; a [0] [1] [2] $a[2]=30; foreach($a as $i) print $i; ? > 10 20 30 OUTPUT: 10 20 30

EXAMPLE 4: array 4. php <? php $a=array(10, 20, 30); print_r ($a); a 10 20 30 [0] [1] [2] ? > ***NOTE: The print_r function outputs human-readable information about a variable and is an excellent tool for quickly viewing array contents OUTPUT:

ASSOCIATIVE ARRAYS DEFINITION: Associative arrays are arrays that have string values as their indices.
![EXAMPLE 1 php temperaturearrayguntur40 vijayawada42 ongole44 print temperatureguntur print temperaturevijayawada print EXAMPLE 1 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole"=>44); print $temperature["guntur"]; print $temperature[“vijayawada "]; print](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-14.jpg)
EXAMPLE 1 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole"=>44); print $temperature["guntur"]; print $temperature[“vijayawada "]; print $temperature[“ongole "]; ? > OUTPUT: 40 42 44 temperature 40 42 [“guntur”] [“vijayawada”] string indices 44 [“ongole”]
![EXAMPLE 2 php temperaturearrayguntur40 vijayawada42 ongole 44 foreachtemperature as kv print temperaturek EXAMPLE 2 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole "=>44); foreach($temperature as $k=>$v) print $temperature[$k];](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-15.jpg)
EXAMPLE 2 <? php $temperature=array(“guntur"=>40, " vijayawada"=>42, "ongole "=>44); foreach($temperature as $k=>$v) print $temperature[$k]; ? > OUTPUT: 40 42 44 temperature 40 42 [“guntur”] [“vijayawada”] string indices 44 [“ongole”]

MULTI-DIMENSIONAL ARRAYS

EXAMPLE 1: <? php $a=array( “guntur”=>array( “ 10 -02 -2018”=>array( 45 ) ) ); echo $a[“guntur”][“ 10 -02 -2018”][0]; ? > a [“guntur”] [“ 10 -02 -2018”] OUTPUT: 45 [0] 45
![EXAMPLE 2 php aarray ravi raj echo a000 EXAMPLE 2: <? php $a=array( “ravi”, “raj” ) ) ); echo $a[0][0][0]; ? >](https://slidetodoc.com/presentation_image_h2/5e06b423e79854bfc2bfe06b26c87e93/image-18.jpg)
EXAMPLE 2: <? php $a=array( “ravi”, “raj” ) ) ); echo $a[0][0][0]; ? > a [0] OUTPUT: ravi [0] [1] ravi raj

EXAMPLE 3: <? php $a=array( 22, “guntur”=>40, “vij”=>array( 99 ) , array( “ravi”, “raj” ) ); echo $a[0]; echo $a[“guntur”]; echo $a[“vij”][0]; echo $a[1][1]; echo $a[“vij”]; ? > a OUTPUT: 224099 raviraj. Array [0] [“guntur”] 22 40 [“vij”] [1] [0] [1] 99 ravi raj

Built-in Array Functions

(i) Function Name: count() Description: It counts all elements in an array(i. e it gets size of an array) or properties in an object. Syntax: int count(array, mode) Parameters: (i) array-Required. It specifies the array or object to count. (ii) mode-Optional. It specifies the mode of the function. Possible values of mode are 0 or 1. EXAMPLE: <? php $a=array(10, 20, 30, 40, 50); echo count($a); ? > OUTPUT: 5

(ii) Function Name: array_search() Description: It searches an array for a value and returns the index. Syntax: array_search(value, array, strict) Parameters: (i) value-Required. It specifies the value to search for. (ii) array-Required. It specifies the array to search in. (iii)strict-Optional. Possible values are true and false. EXAMPLE: <? php $a=array(44, 59, 67, 98, 12); echo array_search(12, $a); ? > OUTPUT: 4

(iii) Function Name: sort() Description: It sorts an array. Syntax: sort(array, sorttype) Parameters: (i) array-Required. It specifies an array to sort. (ii)sorttype-Optional. Specifies how to sort array values. Possible values are SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING. EXAMPLE: <? php $a=array(44, 59, 67, 98, 12); sort($a); foreach($a as $i) print $i; ? > OUTPUT: 12 44 59 67 98

List Of Some Commonly Used Array Functions Function Name Description array_fill() Fills an array with values. array_intersect() Compares array values, and returns the matches. array_merge() Merges one or more arrays into one array_multisort() Sorts multiple or multi-dimensional arrays. array_pop() Deletes the last element of an array_product() Calculates the product of the values in an array.

List Of Some Commonly Used Array Functions Function Name array_push() Description Inserts one or more elements at the end of an array_reverse() Returns an array in the reverse order. array_shift() Removes the first element from an array, and returns the value of the deleted element. array_sum() Returns the sum of the values in an array.

List Of Some Commonly Used Array Functions Function Name Description array_unique() Returns duplicate values from an array_values() Returns all the values of an array. extract() Imports variables into the current symbol table from an array. in_array() Checks if a specified value exists in an array. krsort() Sorts an array by index in reverse order. ksort() Sorts an array by index. list() Assigns variables as if they were an array.

List Of Some Commonly Used Array Functions Function Name Description prev() Rewinds the internal array pointer. range() Creates an array containing a range of elements. reset() Sets the internal pointer of an array to its first element. rsort() Sorts an array in reverse order. uksort Sorts an array by index using a user-defined function. usort Sorts an array by values using a user-defined function.