How to convert numeric value into array in php

PHPz
Release: 2023-04-20 11:04:31
Original
565 people have browsed it

In PHP development, sometimes we need to convert values ​​into arrays to perform some operations or facilitate data processing. This article will introduce how to convert numerical types into arrays in PHP.

  1. Use the str_split function

The str_split function in php can convert a string into a character array. This function can help us convert a numerical string into an array. function. The sample code is as follows:

$num = 123456;
$numArray = str_split($num);
print_r($numArray);
Copy after login

After running the code, the output result is:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
Copy after login
  1. Use preg_split function

Sometimes we need to convert a numerical string according to Splits the specified length into multiple strings and places the strings in an array. PHP's preg_split function can achieve this function through regular expressions. The sample code is as follows:

$num = "1234567890";
$numArray = preg_split("/(?<=\d{3})(?!$)/", $num, -1, PREG_SPLIT_NO_EMPTY); 
print_r($numArray);
Copy after login

The output result is as follows:

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
    [3] => 0
)
Copy after login
  1. Using array functions

In PHP, we can also use array functions to quickly convert values Convert to array. Through the range and array_map functions, we can generate an array from 1 to a specified value. For example, the method to convert 10 into an array is as follows:

$num = 10;
$numArray = array_map(function ($n) { return $n; }, range(1, $num));
print_r($numArray);
Copy after login

The output result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)
Copy after login
  1. Use a for loop

The simplest method is of course through A for loop adds values ​​to the array one by one. For example, the method of converting 10 into an array is as follows:

$num = 10;
$numArray = [];
for ($i = 1; $i <= $num; $i++) {
  array_push($numArray, $i);
}
print_r($numArray);
Copy after login

The output result is as follows:

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

The above are several methods of converting values ​​into arrays in PHP. Developers can choose the appropriate one according to specific needs. method to convert numeric types to arrays.

The above is the detailed content of How to convert numeric value into array in php. For more information, please follow other related articles on the PHP Chinese website!

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