Home Backend Development PHP Problem How to cut an array in php? Introduction to built-in functions

How to cut an array in php? Introduction to built-in functions

Apr 20, 2023 pm 03:06 PM

In PHP, sometimes we need to cut an array according to certain rules, such as cutting according to the value of a certain field, or cutting according to a certain length, etc. At this time, we can use some functions provided in PHP to complete this task. This article will introduce how to use PHP functions to cut arrays.

1. Use the array_chunk() function to cut the array

The array_chunk() function can cut an array according to the specified length, and each cut part is saved in a new array item in. The specific syntax is as follows:

array array_chunk(array $array, int $size, bool $preserve_keys = false)
Copy after login

The meaning of each parameter is as follows:

  • $array: the array to be cut.
  • $size: The number of elements contained in each new array item.
  • $preserve_keys: Whether to retain the key names of the original array.

For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to slice this array according to each array item containing 3 elements, you can use the following code:

$new_array = array_chunk($nums,3);
print_r($new_array);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)
Copy after login
Copy after login

2. Use array_slice () function to slice the array

array_slice() function can slice the array according to the specified index range and return the sliced ​​array. The specific syntax is as follows:

array array_slice(array $array, int $offset, int $length = null, bool $preserve_keys = false)
Copy after login

The meaning of each parameter is as follows:

  • $array: the array to be cut.
  • $offset: The starting index of cutting.
  • $length: The cutting length. If not specified, it will be cut to the end of the array.
  • $preserve_keys: Whether to retain the key names of the original array.

For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to cut this array from the 4th element to the 7th element, you can use the following code:

$new_array = array_slice($nums,3,4);
print_r($new_array);
Copy after login

The output result is as follows:

Array
(
    [0] => 4
    [1] => 5
    [2] => 6
    [3] => 7
)
Copy after login

3. Use The array_filter() function is used to cut the array

The array_filter() function can filter an array according to the specified rules and return an array that meets the conditions. The specific syntax is as follows:

array array_filter(array $array, callable $callback = null, int $flag = 0)
Copy after login

The meaning of each parameter is as follows:

  • $array: the array to be cut.
  • $callback: Filter function, if a function is specified, the function will be used to filter array elements.
  • $flag: Identifier, specifying whether the key relationship of the array remains intact.

For example, if there is an array $nums=['1','2','3','4','5','6','7','8' ,'9'], we want to filter out odd elements from this array, we can use the following code:

$new_array = array_filter($nums,function($var){return ($var%2==1);});
print_r($new_array);
Copy after login

The output result is as follows:

Array
(
    [0] => 1
    [2] => 3
    [4] => 5
    [6] => 7
    [8] => 9
)
Copy after login

4. Use foreach loop to cut the array

In addition to using the functions provided by PHP to cut the array, we can also use the foreach loop to complete this task. The specific code is as follows:

$nums=['1','2','3','4','5','6','7','8','9'];
$new_array=array();
$temp_array=array();
foreach ($nums as $key=>$value){
    $temp_array[]=$value;
    if(($key+1)%3==0||$key==count($nums)-1){
        $new_array[]=$temp_array;
        $temp_array=array();
    }
}
print_r($new_array);
Copy after login

The output result is as follows:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)
Copy after login
Copy after login

The above are the four methods of using PHP to cut arrays. You can choose the appropriate method to complete the task according to the actual situation.

The above is the detailed content of How to cut an array in php? Introduction to built-in functions. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)