PHP: Array 矩阵
Array
- An array is a special variable using which we can store multiple values in a single variable
- Names of 5 employee can be stored using 5 variables. If the case is for 50 employees, then array is the solution
- An array can hold multiple values under a single name which can be accessed using the index position
- Array can be created using array(); construct
- The values in an array can be either an integer, float, string, object or even another array
- An array can hold data of mixed types
- No restriction in the number of values an array can hold
- Each value in an array is termed as element/value. Each
element has its own unique index
Advantages of Array
- Easy to manipulate
- Easy to work with many values
- Can use many built-in array functions like sort, search, merge etc
Displaying the array as an output
Displaying output on the screen is differ with the
memory value on the computer i.e. The value of $a when output is 10 but the value in the memory is 11
1 | echo (); |
Example 1
1 | <?php |
Array( [0] => volvo[1] => mercedes[2] => ferrari[3] => lamborghini )
Array Functions
Array Function | Description |
---|---|
array() | The function is used to create an array |
array_change_key_case() | It returns an array with all keys in lowercase or uppercase |
array_chunk() | It splits an array into chunks of arrays |
array_combine() | It is used to combine two arrays and creates one array for keys and another array for its values |
array_count_values() | It returns the count of all the array values |
array_diff() | The function compares array values and returns the difference |
array_change_key_case(var, CASE);
Example 1
1 | <?php |
Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
Example 2
1 | <?php |
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )
Example 3
1 | <?php |
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
Array( [s40] => volvo[amg] => mercedes[sauber] => ferrari[sil] => lamborghini )
Array( [S40] => volvo[AMG] => mercedes[SAUBER] => ferrari[SIL] => lamborghini )
array_flip()
Example 1
1 | <?php |
before flip:
Array( [b] => a [d] => c )
after flip:
Array( [a] => b [c] => d )
array_chunk(var, count)
Example 1
1 | <?php |
before flip:
Array( [b] => a [d] => c )
after flip:
Array( [a] => b [c] => d )
array_combine(arr1, arr2)
Example 1
1 | <?php |
Array( [user1] => John [user2] => Alice [user3] => Ravi [user4] => Lim )
Both must have samenumber of arguments!
array_count_values(arr)
Example 1
1 | <?php |
Array( [volvo] => 1 [mercedes] => 1 [ferrari] => 2 )
array_diff(arr1, arr2)
Example
1 | <?php |
Array( [2] => ferrari[3] => ferrar )
Types of array
Indexed array
- Stores each array element with a numeric index. array
Example 1
1 | <?php |
Name list: Pinky, Tom, Prince
Example 2
1 | <?php |
Pinky Tom Prince
Associative array
- Stores each array element as a key to value as a pair. Key can be a user defined string
Example 1
1 | <?php |
Pinky Tom Prince
Example 2
1 | <?php |
Name : Pinky Department : Testing
Name : Tom Department : Development
Name : Prince Department : Admin
Multidimensional array
- Element of an array can contain one or more array
Example
1 | <?php |
1 | <?php |
1 Pinky Testing
2 Tom Development
3 Prince Admin
Convert ARRAY to STRING
Syntax
1 | implode([separator,]$arrayname) |
- The separator specifies what is to be inserted between the array elements
- In implode function, the separator is optional
- The default separator is” “
Example
1 | <?php |
Reading Books :: Playing Golf :: Watching Tennis ::Dancing
Convert STRING to ARRAY
Syntax
1 | explode(separator,string[,limit]) |
- The separator will specify where to break the string. It is mandatory
- Limit specifies number of elements to return. This parameter is optional
Example 1
1 | <?php |
Array([0]=>Golf[1]=>Cricket[2]=>Tennis[3]=>Football[4]=>Hockey)
Example 2
1 | <?php |
Array([0]=>Golf,Cricket,Tennis,Football,Hockey)
Example 3
1 | <?php |
Array([0]=>Golf,Cricket,Tenni[1]=>,Football,Hockey)
Array Sorting
Functions used to sort
- an array in a particular orderare
- Sort by default based on value
- asortbased pnvalue
- ksortbased on key
- rsort(opposite)
- arsort(opposite)
- krsort(opposite)
Example
1 | <?php |
a
b
c
d