CodesJava

Easy learning with example program codes

PHP Array Functions


PHP Array Functions:
Array Functions are the built-in functions provided by PHP to operate and access the elements of array.

PHP array() function:
This function is used to create and return the indexed, associative and multidimensional arrays.

Syntax

array array ([ mixed $... ] )

Example 1

<!DOCTYPE html>
<html>
<body>
 
<?php
$size = array
  (
  array("Pramod",45,5),
  array("Ben",65,4),
  array("Santosh",50,6),
  );
echo $size[0][0].": Weight: ".$size[0][1].", Height: ".$size[0][2].".<br>";
 
?>
 
</body>
</html>

Output

Pramod: Weight: 45, Height: 5.

PHP array_change_key_case() function:
This function is used to change the case of the keys of an associative array.
Syntax

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

Example 2

<!DOCTYPE html>
<html>
<body>
 
<?php    
$weight=array("Rahul"=>"50","Vibha"=>"45","Santa"=>"80");    
print_r (array_change_key_case($weight,CASE_UPPER));   
?>	
 
</body>
</html>

Output

Array ( [RAHUL] => 50 [VIBHA] => 45 [SANTA] => 80 )

PHP array_chunk() function:
This function is used to split array into parts.

Syntax

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

Example 3

<!DOCTYPE html>
<html>
<body>
 
<?php    
$weight=array("Rahul"=>"50","Vibha"=>"45","Santa"=>"80");    
print_r(array_chunk($weight,2));   
?>	
 
</body>
</html

Output

Array ( [0] => Array ( [0] => 50 [1] => 45 ) [1] => Array ( [0] => 80 ) )

PHP count() function
This function is used to count the number of elements in an array.
Syntax

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

Example 4

<!DOCTYPE html>
<html>
<body>
 
<?php    
$flowers=array("lily","lotus","rose","Marigold");    
echo count($flowers);    
?>	
 
</body>
</html>

Output

4

PHP sort() function
This function is used to sort the elements of an array.
Syntax

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

Example 5

<!DOCTYPE html>
<html>
<body>
 
<?php    
$numbers=array("100","160","20","67");    
sort($numbers);  
foreach( $numbers as $n )    
{    
  echo "$n<br />";    
}    
?>	
 
</body>
</html>

Output

20
67
100
160

PHP array_reverse() function
This function is used to reverse the order of the elements of an array.
Syntax

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

Example 6

<!DOCTYPE html>
<html>
<body>
 
<?php    
$flowers=array("lily","lotus","rose","Marigold");   	 
$reverseflowers=array_reverse($flowers);  
foreach( $reverseflowers as $f )    
{    
echo "$f<br />";    
}    
?>    
 
</body>
</html>

Output

Marigold
rose
lotus
lily

PHP array_search() function
This function is used to search a value in an array and to return the key of that value in case of a successful search.
Syntax

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

Example 7

<!DOCTYPE html>
<html>
<body>
 
<?php    
$flowers=array("lily","lotus","rose","Marigold");   	 
$key=array_search("rose",$flowers);  
echo $key;
?>	
 
</body>
</html>

Output

2

PHP array_intersect() function
This function is used to return the matched elements of two array.
Syntax

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

Example 8

<!DOCTYPE html>
<html>
<body>
 
<?php    
$J_name=array("Sohan","Jen","John","Janny");    
$F_name=array("Flora","Fatima","Sohan","Flaira");    
$odd_name=array_intersect($J_name,$F_name);  
foreach( $odd_name as $n )    
{    
echo "$n<br />";    
}    
?>    
 
</body>
</html>

Output

Sohan
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki