The php array is divided into array values and array keys. The following small series will summarize the common operations for array values in php: if you need to add values to the array, determine the values in the array, and delete specific array values, you can refer.
Php deletes a specific array value
First
The Code is as follows: |
Copy code |
Var_dump ($ context ['linktree']); |
Get
The Code is as follows: |
Copy code |
Array (3 ){ [0] => Array (2 ){ ["Url"] => String (52) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php" ["Name"] => String (28) "City of Steam Official Forum" } [1] => Array (2 ){ ["Url"] => String (55) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php?c1 ″ ["Name"] => String (28) "City of Steam Official Forum" } [2] => Array (2 ){ ["Url"] => String (62) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php? Board = 4.0 ″ ["Name"] => String (12) "Announcement" } } |
I want to remove the middle one.
Use: unset ($ context ['linktree'] ['1']);
Result:
The Code is as follows: |
Copy code |
Array (2 ){ [0] => Array (2 ){ ["Url"] => String (52) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php" ["Name"] => String (28) "City of Steam Official Forum" } [2] => Array (2 ){ ["Url"] => String (62) "http: // 127.0.0.1/testforum.cityofsteam.com/index.php? Board = 4.0 ″ ["Name"] => String (12) "Announcement" } } |
One [1] is missing.
Let the Middle 1 be automatically numbered:
The Code is as follows: |
Copy code |
Array ([0] => apple [1] => banana [3] => dog)
|
However, the biggest disadvantage of this method is that the array index is not rebuilt, that is, the third element of the array is gone.
After checking the information, PHP provided this function, but it was indirect. This function is array_splice ().
For ease of use, I encapsulated a function for your convenience:
The Code is as follows: |
Copy code |
<? Php Function array_remove (& $ arr, $ offset) { Array_splice ($ arr, $ offset, 1 ); } $ Arr = array ('apple', 'Banana ', 'cat', 'Dog '); Array_remove ($ arr, 2 ); Print_r ($ arr ); ?> |
After testing, we can know that the element 2 is actually deleted and the index is re-created.
Program running result:
The Code is as follows: |
Copy code |
Array ([0] => apple [1] => banana [2] => dog) |
Php determines the value in the array
There are special functions. Do not use a for loop. system functions can achieve quick search:
In_array
(PHP 4, PHP 5)
In_array-check whether a value exists in the array
Description
Bool in_array (mixed $ needle, array $ haystack [, bool $ strict])
Search for needle in haystack. If yes, TRUE is returned. Otherwise, FALSE is returned.
If the value of the third strict parameter is TRUE, the in_array () function checks whether the needle type is the same as that in haystack.
Note: If the needle is a string, the comparison is case sensitive.
Note: Before PHP version 4.2.0, needle cannot be an array.
Example 292. in_array ()
The Code is as follows: |
Copy code |
<? Php $ OS = array ("Mac", "NT", "Irix", "Linux "); If (in_array ("Irix", $ OS )){ Echo "Got Irix "; } If (in_array ("mac", $ OS )){ Echo "Got mac "; } ?>
|
The second condition fails, because in_array () is case sensitive, so the above program is shown:
Got Irix
Example 293. in_array () Strict type check example
The Code is as follows: |
Copy code |
<? Php $ A = array ('1', 12.4, 1.13 ); If (in_array ('12. 4', $ a, true )){ Echo "'12. 4' found with strict checkn "; } If (in_array (1.13, $ a, true )){ Echo "1.13 found with strict checkn "; } ?> The above example will output: 1.13 found with strict check |
Example 294. in_array () uses arrays as needle
The Code is as follows: |
Copy code |
<? Php $ A = array ('P', 'H'), array ('P', 'R'), 'O '); If (in_array (array ('P', 'H'), $ )){ Echo "'ph' was foundn "; } If (in_array (array ('F', 'I'), $ )){ Echo "'fi 'was foundn "; } If (in_array ('O', $ )){ Echo "'O' was foundn "; } ?> The above example will output: 'Ph 'was found 'O' was found |
Add a value to an array
We can use functions to insert one or more elements into an array or add them directly.
(1) Add data directly to the array. The subscript of the new element starts after the maximum value of the original array subscript.
(2) The array_unshift () function adds one or more elements at the beginning of the array.
Syntax:
Int array_unshift (array & array, mixed var [, mixed...]);
Array_unshift () inserts the input elements into the beginning of the array. Elements are inserted as a whole, and the incoming elements are in the same order. The names of all numeric keys start from 0 and remain unchanged.
(3) The array_push () function adds one or more units to the end of the array.
Syntax:
Int array_push (array & array, mixed var [, mixed...]);
Array_push () treats array as a stack and adds the passed variables to the end of array. This function returns the total number of new elements in the array. An example of adding data to an array is as follows.
Example:
The Code is as follows: |
Copy code |
<? Php $ Shili = array ("1", "2", "3", "4 ″); $ Shili [] = 5; // Add data directly Print_r ($ shili ); Echo "<br> "; $ Shili2 = array ("m", "n "); Array_unshift ($ shili2, "o", "p"); // Add the element at the beginning of the array Print_r ($ shili2 ); Echo "<br> "; $ Shili3 = array ("Php "); Array_push ($ shili3, "MySQL", "Apache"); // Add the element to the end of the array Print_r ($ shili3 ); ?> Result: Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) Array ([0] => o [1] => p [2] => m [3] => n) Array ([0] => Php [1] => MySQL [2] => Apache)
|