Home > Backend Development > PHP Problem > PHP array learning: extract digital elements and splice them into a new array

PHP array learning: extract digital elements and splice them into a new array

青灯夜游
Release: 2023-03-12 08:22:01
Original
3609 people have browsed it

In the previous article, we introduced the method of emptying and deduplicating arrays, that is, deleting empty elements and duplicate elements in the array. If you are interested, you can click on the link to read → " How to remove blanks in PHP array learning Or repeated elements》. This time we continue the study and practice of PHP arrays and talk about how to filter arrays and extract numerical elements.

→Related recommendations: PHP array learning series summary (continuously updated~)

The main content of today’s article is: Utilization PHP to filter the array, filter out the numeric elements (numeric values ​​or numeric strings), and then combine them into a new array and return it.

Below we will introduce two implementation methods to you. First, we will start with the familiar method "foreach loop", and then introduce a method of filtering arrays using PHP's built-in functions.

Method 1: Use the foreach statement

<?php 
$array = array("php", 11, &#39;&#39;, 12, "PHP中文网",13,"green",2021,"mysql","14",15); 
foreach($array as $value){
	if(is_numeric($value)){
		$result[]=$value;
	}
}
var_dump($result);
?>
Copy after login

Analyze the code:

  • Use the foreach statement to traverse the array, in each loop Assign the key value to $value;

  • Use the is_numeric() function to detect $value Whether it is a number or a string of numbers;

  • If it is a number or a string of numbers, store $value in the $results array .

In this way, all the numeric elements in the $results array are the numeric elements in the $array array. Using var_dump($result), the output result is:

PHP array learning: extract digital elements and splice them into a new array

Method 2: Using the array_filter() function

In the previous article, we already know that the array_filter() function can use a callback function To filter the elements in the array, the array elements will be passed to the callback function for processing.

Give the implementation code directly:

<?php 
$array = array("php", 11, &#39;&#39;, 12, "PHP中文网",13,"green",2021,"mysql","14",15); 
function filter_number($value){
	if(is_numeric($value)){
		return TRUE;
	}
}

$result=array_filter($array,"filter_number");
var_dump($result);
?>
Copy after login

The output result is:

PHP array learning: extract digital elements and splice them into a new array

Let’s take a look at the array_filter() function

# The ##array_filter() function iterates through each value in the array and passes each key value to a user-defined function or callback function; if the callback function returns true, the current key value in the input array is returned to the result array ( The array key names remain unchanged). The syntax format is:

array_filter ($array , function callbackfn ($value[, $key]),$mode)
Copy after login
  • $array: array object, cannot be omitted.

  • function callbackfn ($value[, $key]): Callback function, which can be omitted; if the callback function is omitted, null values ​​will be filtered by default.

  • $mode: can be omitted, determines the parameter form received by the callback:

    • ARRAY_FILTER_USE_KEY - callback accepts the key name as the only parameter

    • ARRAY_FILTER_USE_BOTH - callback accepts both key name and key value

    • The default value is 0, and only the value is passed as the only parameter of the callback callback.


Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning: extract digital elements and splice them into a new array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template