your image

PHP Array Functions - javatpoint

javapoint
Related Topic
:- PHP programming languages

PHP Array Functions

PHP provides various array functions to access and manipulate the elements of array. The important PHP array functions are given below.

1) PHP array() function

PHP array() function creates and returns an array. It allows you to create indexed, associative and multidimensional arrays.

Syntax

  1. array array ([ mixed $... ] )  

Example

 

2. Objects in PHP | Build a CMS using OOP PHP tutorial MVC [2020]

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. echo "Season are: $season[0], $season[1], $season[2] and $season[3]";    
  4. ?>    

Output:

Season are: summer, winter, spring and autumn

2) PHP array_change_key_case() function

PHP array_change_key_case() function changes the case of all key of an array.

Note: It changes case of key only.

Syntax

  1. array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )  

Example

  1. <?php    
  2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
  3. print_r(array_change_key_case($salary,CASE_UPPER));   
  4. ?>    

Output:

Array ( [SONOO] => 550000 [VIMAL] => 250000 [RATAN] => 200000 )

Example

  1. <?php    
  2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
  3. print_r(array_change_key_case($salary,CASE_LOWER));   
  4. ?>    

Output:

 

Array ( [sonoo] => 550000 [vimal] => 250000 [ratan] => 200000 )

3) PHP array_chunk() function

PHP array_chunk() function splits array into chunks. By using array_chunk() method, you can divide array into many parts.

Syntax

  1. array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )  

Example

  1. <?php    
  2. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");    
  3. print_r(array_chunk($salary,2));   
  4. ?>    

Output:

Array ([0] => Array ( [0] => 550000 [1] => 250000 )[1] => Array ( [0] => 200000 ))

4) PHP count() function

PHP count() function counts all elements in an array.

 

Syntax

  1. int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  

Example

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. echo count($season);    
  4. ?>    

Output:

 

4

5) PHP sort() function

PHP sort() function sorts all the elements in an array.

Syntax

  1. bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  

Example

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. sort($season);  
  4. foreach( $season as $s )    
  5. {    
  6.   echo "$s<br />";    
  7. }    
  8. ?>    

Output:

autumnspringsummerwinter

6) PHP array_reverse() function

PHP array_reverse() function returns an array containing elements in reversed order.

Syntax

  1. array array_reverse ( array $array [, bool $preserve_keys = false ] )  

Example

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. $reverseseason=array_reverse($season);  
  4. foreach( $reverseseason as $s )    
  5. {    
  6.   echo "$s<br />";    
  7. }    
  8. ?>    

Output:

autumnspringwintersummer

7) PHP array_search() function

PHP array_search() function searches the specified value in an array. It returns key if search is successful.

 

Syntax

  1. mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )  

Example

  1. <?php    
  2. $season=array("summer","winter","spring","autumn");    
  3. $key=array_search("spring",$season);  
  4. echo $key;    
  5. ?>    

Output:

2

8) PHP array_intersect() function

PHP array_intersect() function returns the intersection of two array. In other words, it returns the matching elements of two array.

Syntax

  1. array array_intersect ( array $array1 , array $array2 [, array $... ] )  

Example

  1. <?php    
  2. $name1=array("sonoo","john","vivek","smith");    
  3. $name2=array("umesh","sonoo","kartik","smith");    
  4. $name3=array_intersect($name1,$name2);  
  5. foreach( $name3 as $n )    
  6. {    
  7.   echo "$n<br />";    
  8. }    
  9. ?>    

Output:

sonoosmith

Comments