PHP Array Functions PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP Array Functions
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
- array array ([ mixed $... ] )
Example
- <?php
- $season=array("summer","winter","spring","autumn");
- echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
- ?>
Output:
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
- array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
Example
- <?php
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
- print_r(array_change_key_case($salary,CASE_UPPER));
- ?>
Output:
Example
- <?php
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
- print_r(array_change_key_case($salary,CASE_LOWER));
- ?>
Output:
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
- array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
Example
- <?php
- $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000");
- print_r(array_chunk($salary,2));
- ?>
Output:
[0] => Array ( [0] => 550000 [1] => 250000 )
[1] => Array ( [0] => 200000 )
)
4) PHP count() function
PHP count() function counts all elements in an array.
Syntax
- int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )
Example
- <?php
- $season=array("summer","winter","spring","autumn");
- echo count($season);
- ?>
Output:
5) PHP sort() function
PHP sort() function sorts all the elements in an array.
Syntax
- bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
Example
- <?php
- $season=array("summer","winter","spring","autumn");
- sort($season);
- foreach( $season as $s )
- {
- echo "$s<br />";
- }
- ?>
Output:
spring
summer
winter
6) PHP array_reverse() function
PHP array_reverse() function returns an array containing elements in reversed order.
Syntax
- array array_reverse ( array $array [, bool $preserve_keys = false ] )
Example
- <?php
- $season=array("summer","winter","spring","autumn");
- $reverseseason=array_reverse($season);
- foreach( $reverseseason as $s )
- {
- echo "$s<br />";
- }
- ?>
Output:
spring
winter
summer
7) PHP array_search() function
PHP array_search() function searches the specified value in an array. It returns key if search is successful.
Syntax
- mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
Example
- <?php
- $season=array("summer","winter","spring","autumn");
- $key=array_search("spring",$season);
- echo $key;
- ?>
Output:
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
- array array_intersect ( array $array1 , array $array2 [, array $... ] )
Example
- <?php
- $name1=array("sonoo","john","vivek","smith");
- $name2=array("umesh","sonoo","kartik","smith");
- $name3=array_intersect($name1,$name2);
- foreach( $name3 as $n )
- {
- echo "$n<br />";
- }
- ?>
Output:
smith