Arrays in PHP are an indispensable part of our application development. Now I will share with you some of my own operations on learning PHP arrays. I hope that students who need to know more can refer to them.
For Web programming, the most important thing is accessing, reading and writing data. There may be many storage methods, including strings, arrays, files, etc. Arrays can be said to be one of the more important methods in PHP data applications. There are many array functions in PHP. The following is a summary of what I learned so that I can learn from them in the future. . Array definition The definition of an array is defined using the array() method. You can define an empty array:
The compact() function is used to convert two or more variables into arrays, including array variables of course. The parameter is the name of the variable rather than the full name with $. The opposite function is extract(). As the name suggests, it converts the array into a single string, with the key value as its string name and the array value as the string value.
Running results:
Array (
[number] => 1,3,5,7,9
[string] => I'm PHPer
[array] => Array ( [0] => And [1] => You? )
)
array_combine()
array_combine()——Reorganize two arrays into one array, one as the key value and the other as the value: array array_combine (array $keys, array $values)
I won’t go into details about the array_combine function, everyone will understand it after reading it.
Running result:
Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
range()
range() function - creates an array of a specified range:
Run result:
0=>50
1=>120
2=>180
3=>240
4=>380 while loop traversal While loop traversal is generally combined with the list function. The following is an example
}
?>
Run result:
0 20 40 60 80 100 120 140 160 180 200 220
4. Array pointer operations
Involved functions include reset, prev, end, next, current, and each.
Example 1: next and prev
The code is as follows
Copy code
";
echo prev($speed);//Output the previous position array value
echo " ";
echo reset($speed);//Reset the pointer of the array and point the pointer to the starting position
echo " ";
echo end($speed);//Output the array value of the last position
echo " ";
?>
Run result:
0220
200
0
220
Example 2: each function pointer operation
The code is as follows
Copy code
";
echo "The speed of 0 gear is".current(each($speed))." ";
echo "The speed of 1st gear is".current(each($speed))." ";
echo "The speed of 2nd gear is".current(each($speed))." ";
echo "The speed of 3rd gear is".current(each($speed))." ";
echo "The speed of 4th gear is".current(each($speed))." ";
echo "The speed of 5th gear is".current(each($speed))." ";
echo "Use each function to move the array pointer and traverse the array ";
reset($speed);//This is to point the array pointer to the beginning of the array
while(list($key,$value)=each($speed)){
echo $key."=>".$value." ";
}
?>
Run result:
Each implements pointer movement down
The speed of gear 0 is 0
The speed of 1st gear is 40
The speed in 2nd gear is 80
The speed in 3rd gear is 120
The speed in 4th gear is 160
The speed in 5th gear is 200
Use each function to move the array pointer and traverse the array
0=>0
1=>40
2=>80
3=>120
4=>160
5=>200
5. Array addition and deletion operations
Add array members
Example 1: $num[] = value is directly assigned and appended to the end of the array:
[code
Run result:
Use the array_pad function to add members to the end of the array
Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 200 )
array_pad can also fill the array header
Array ( [0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5] => 120 [6] => 160 [7] => 200 )
Example 3: Add push operation (array_push):
The code is as follows
Copy code
$num = array(1=>80,2=>120,3=>160);
array_push($num,200,240,280);//You can append by yourself, directly at the end of the array
print_r($num);
?>
Run result:
Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )
Example 4: array_unshift() adds array members at the beginning
The code is as follows
Copy code
$num = array(1=>80,2=>120,3=>160);
array_unshift($num,0,40);//You can add it yourself, directly at the end of the array
print_r($num);
?>
Run result:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
Note: After using the array_unshift() function, the key values of the array will start from 0!
Delete array members
Example 1: The unset() command deletes array members or arrays:
Notice: Use of undefined constant is_array - assumed 'is_array' in H:wampwwwtestingeditorplustest.php on line 21
The unset command cannot delete the entire array
Example 2: array_splice() function deletes array members
The code is as follows
Copy code
$a=array("red", "green", "blue", "yellow"); <🎜>
count ($a); //Get 4 <🎜>
array_splice($a,1,1); //Delete the second element <🎜>
count ($a); //Get 3 <🎜>
echo $a[2]; //get yellow <🎜>
echo $a[1]; //get blue <🎜>
?>
Example 3: array_unique deletes duplicate values in the array:
结果:
Array ( [0] => a [1] => b [2] => c [3] => d )
Array ( [0] => d [1] => c [2] => b [3] => a )
sort()、rsort()函数对数组进行从低到高的排序,返回结果为bool值;
asort()、arsort()函数是保留键值的排序,排序后键值不重新索引。
实例二:将数组顺序打乱——shuffle()函数:
The result is dynamic:
Array ( [0] => c [1] => a [2] => d [3] => b )
The result of shuffle is a bit random and different every time it is refreshed.
Example 3: array_reverse() array reverse:
Array ( [2] => sort1 [0] => sort2 [3] => sort4 [1] => Sort5 )
natsort() and natcasesort() perform natural sorting on arrays, which is the normal sorting algorithm using numbers. natcasesort ignores case.
Example 5: Sort the array by key value ksort():
The code is as follows
Copy code
$array = array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4");
ksort($array);//Sort from low to high
print_r($array);
?>
Result:
Array ( [1] => sort2 [2] => sort1 [3] => sort4 [4] => Sort5 )
Note: The ksort() function re-indexes.
8. Other uses of arrays
$
The code is as follows
Copy code
array = array ('A', 'B', 'C' );
//Use int array_unshift(array $array,mixed variable[,mixed variable...]) to add elements to the head of the array
array_unshift ( $array, 'E', 'F', 'G' );
var_dump ( $array );
$array = array ('A', 'B', 'C' );
//Use int array_push(array $array,mixed variable[,mixed variable...]) to add elements at the end of the array
array_push ( $array, 'E', 'F', 'G' );
var_dump ( $array );
$array = array ('A', 'B', 'C' );
//Use mixed array_shift(array $array) to delete elements at the head of the array
array_shift ( $array );
var_dump ( $array );
$array = array ('A', 'B', 'C' );
//Use mixed array_pop(array $array) to delete elements at the end of the array
array_pop ( $array );
var_dump ( $array );
/*
* Search for a specific value in the array, return TRUE if found otherwise return FALSE
* boolean in_array(mixed needle,array haystack[,boolean strict])
* Find a specified key in the array, return TRUE if found, otherwise return FALSE
* boolean array_eky_exists(mixed key,array array)
* Search for a specific value in the array, return TRUE if found otherwise return FALSE
* boolean array_search(mixed needle,array haystack[,boolean strict])
* Get a new array composed of all keys of the array
* array array_keys(array array[,mixed search_value])
* Get a new array composed of all values in the array
* array array_values(array array)
* Determine array size
* integer count(array array[,int mode])
* integer sizeof(array array[,int mode])
* Count the frequency of occurrence of array elements
* array array_count_values(array array)
* Remove duplicate values from the array and return an array composed of unique values
* array array_unique(array array)
* Reverse the order of array elements. If preserve_key is TRUE, the order of array key values will remain unchanged
* array array_reverse(array array[,boolean preserve_key])
* Replace array keys and values
* array array_flip(array array)
* Array order sorting, sort_flags parameter is optional, default behavior
* SORT_NUMBERIC, sort by numerical value, useful for sorting integers or floating point numbers
*SORT_REGULAR, sort by ASCII value
* SORT_STRING, sorted in the correct order known by people closest to you
* The key value order of the asort function remains unchanged
* void sort(array array[,int sort_flags])
* void asort(array array[,int sort_flags])
* Sort the array in reverse order, the sort_flags parameter is optional, and the default behavior is
* SORT_NUMBERIC, sort by numerical value, useful for sorting integers or floating point numbers
*SORT_REGULAR, sort by ASCII value
* SORT_STRING, sorted in the correct order known by people closest to you
* The key value order of the arsort function remains unchanged
* void rsort(array array[,int sort_flags])
* void arsort(array array[,int sort_flags])
* Natural sorting of arrays
* void natsort(array array)
* Case-insensitive natural sorting
* void natcasesort(array array)
* Sort array by key value
* boolean ksort(array array[,int sort_flags])
* Sort the array in reverse order by key value
* boolean krsort(array array[,int sort_flags])
* Sort according to user-defined order
* void usort(array array,callback function_name)
* Merge the arrays together to return a combined array. The back of array_merge covers the front, array_merge_recursive merges together
* array array_merge(array array1[array array2...])//More than one
* array array_merge_recursive(array array1,array array2[,array…])//More than two
* Keys and values form a new array
* array array_combine(array key,array value)
* Return a part of the array, starting from offset and ending at offset+length
* array array_slice(array array, int offset [,int length])
* Delete all elements starting from offset and ending at offset+length, and return the deleted elements in the form of an array
* array array_splice(array, int offset [,int length[,array peplacement]])
* Find the intersection of arrays, the key value is the key value in the first array
* array array_intersect(array array1,array array2[,arrayN……])
* Find the intersection of arrays that contains equal key values, and the key value is the key value in the first array
* array array_intersect_assoc(array array1,array array2[,arrayN……])
* Find the difference set of arrays, the first array has values that are not found in other arrays
* array array_diff(array array1,array array2[,arrayN……])
* Find the difference set of arrays. The first array contains equal key values in values that are not found in other arrays
* array array_diffassoc(array array1,array array2[,arrayN……])
* Return one or more key values in the array
* mixed array_rand(array array[,int num_entries])
* Instant shuffle function
* void shuffle(array input_array)
* Sum the values in the array
* mixed array_sum(array array);
* Decompose the array into a multi-dimensional array, which contains size elements
* array array_chunk(array array, int size [,boolean preserve_keys])
*/
http://www.bkjia.com/PHPjc/632681.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632681.htmlTechArticleArrays in php are an indispensable part of our application development. Now let me learn about php myself. I would like to share some operations on arrays with you. I hope students who need to know more can go in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn