1 of 24

PHP ARRAY

PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.

2 of 24

PHP ARRAY

A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

3 of 24

Advantage of PHP Array

  • Less Code: We don't need to define multiple variables
  • Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
  • Sorting: We can sort the elements of array.

4 of 24

TYPES OF ARRAY

There are 3 types of array in PHP

  • 1. Indexed Array
  • 2. Associative Array
  • 3. Multidimensional Array

5 of 24

PHP Indexed Array�

  • 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.

6 of 24

PHP Indexed Array�

  • Syntax for creating array in PHP

Type 1 :

$season=array("summer","winter","spring","autumn");  

Type 2 :

$season[0]="summer";  

$season[1]="winter";  

$season[2]="spring";  

$season[3]="autumn";  

7 of 24

PHP Associative Array

  • PHP allows you to associate name/label with each array elements in PHP using => symbol.

  • In it every value can be assigned with a user-defined key of string type.

  • It’s differ from numeric array in the sense that associative arrays use descriptive names for id keys.

8 of 24

PHP Associative Array

  • Syntax for creating array in PHP

Type 1 :

$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");  

 

Type 2 :

$salary["Sonoo"]="350000";  

$salary["John"]="450000";  

$salary["Kartik"]="200000";  

9 of 24

PHP Multidimensional Array

  • PHP multidimensional array is also known as array of arrays. 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.

  • The advantage of multidimensional array is that they allow us to group related data together

10 of 24

PHP Multidimensional Array

  • Syntax for creating array in PHP

$emp = array  

  (  

  array(1,"sonoo",400000),  

  array(2,"john",500000),  

  array(3,"rahul",300000)  

  ); 

  

11 of 24

PHP extract() function

  • The extract( ) function is an inbuilt function of PHP, and it is used to import variables into the local symbol table from an array  

12 of 24

PHP extract() function

<?php  

    $info= array( );  

    $info['name']= "ajeet";  

    $info['office']= “msbte";  

    $info['city']= “mumbai";  

    $info['profile']= "HR";  

    extract($info);  

    echo $name;  

    echo $city;  

?>  

13 of 24

PHP compact() function

  • The compact( ) function is an inbuilt function of PHP, and it is used to create an array from variables and their values  

14 of 24

PHP compact() function

<?php

$var1 = "PHP";

$var2 = "JAVA";

$var3 = compact("var1","var2");

print_r($var3);

?>

15 of 24

PHP implode() function

  • PHP implode() is a string function, which joins the array elements in a string. It is a binary-safe function.
  • In implode() function, parameters can be passed in any order.  
  • The implode() function works same as the join() function and returns a string created from the elements of the array. Basically, this function joins all elements of array in one string.

16 of 24

PHP implode() function

Syntax :

Type 1.

string implode(separator, array);

Type 2.

implode (string $glue, array $pieces)

Type 3.

implode (array $pieces)

Separator ( , : + - etc.)

17 of 24

PHP explode() function

  • PHP explode() function breaks a string into an array.  
  • The explode() function is a built in function in PHP used to split a string in different strings.
  • The explode() function splits a string based on a string delimiter.

18 of 24

PHP explode() function

Syntax :

array explode(separator, OriginalString, NoOfElements);

19 of 24

PHP array_flip() function

Syntax :

Array_flip(array);

20 of 24

PHP array_flip() function Example

<?php

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");�$result=array_flip($a1);�print_r($result);

?>

21 of 24

PHP unset() function

The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

22 of 24

PHP unset() function

The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

syntax :- void unset ( mixed $var , mixed $vars )

23 of 24

PHP unset() function Example

<?php

$a = "Welcome TO PHP!";

echo "The value of 'a' before unset: " . $a . "<br>";

unset($a);

echo "The value of 'a' after unset: " . $a;

?>

24 of 24

The value of 'a' before unset: Welcome To PHP!��Warning: Undefined variable $a in C:\xampp\htdocs\php\array\unset_function.php on line 5�The value of 'a' after unset:

PHP unset() function Output