During the development process, when an array is null, it is wrong to directly judge whether empty is null, because when this value is a multi-dimensional number, empty results have a value, use array
During the development process, when an array is null, it is wrong to directly judge whether empty is null, because when this value is a multi-dimensional number, empty results have a value, use the array_filter function to easily remove multi-dimensional null values. the subscript of the array has not changed. paste the following online usage:
PHP code
- $ Entry = array (
- 0 => '9th Street php ',
- 1 => false,
- 2 => 1,
- 3 => null,
- 4 => '',
- 5 => 'http: // www.9streets.cn ',
- 6 => '0'
- );
- Print_r (array_filter ($ entry ));
- ?>
- The output result of the above code is:
- Array
- (
- [0] => Ninth Street PHP
- [2] => 1
- Http://www.9streets.cn
- )
Let's see how array_filter is defined:
The array_filter () function uses the callback function to filter elements in the array. if the custom filter function returns true, the current value of the operated array is included in the returned result array, and form a new array of results. If the original array is an associated array, the key name remains unchanged.
PHP code
- Array_filter (array, function) array is required. Specifies the input array. When the function does not have the second parameter, array_filter () deletes all entries in the array that are equivalent to FALSE. this is the essence of filtering the blank elements in the array.
For better understanding, paste the following online example:
PHP code
- Function myfunction ($ v)
- {
- If ($ v = "Horse ")
- {
- Return true;
- }
- Return false;
- }
- $ A = array (0 => "Dog", 1 => "Cat", 2 => "Horse ");
- Print_r (array_filter ($ a, "myfunction"); // output Array ([2] => Horse); note that the key value has not changed, if you want to re-sort the new array, you can use the sort function.