Home  >  Article  >  Backend Development  >  How to convert numbers into array in php

How to convert numbers into array in php

藏色散人
藏色散人Original
2021-09-22 10:12:382799browse

php method to convert numbers into arrays: 1. Create a PHP sample file; 2. Define a string containing numbers; 3. Pass "for($i=0;$i

How to convert numbers into array in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to convert numbers into arrays in php?

PHP Convert the numbers in the string into an array

$str ='现在是2019年11月18日下午17点25分';
$result='';
$arr=[];
for($i=0;$i<strlen($str);$i++){
    if(is_numeric($str[$i])){
        $result.=$str[$i];
    }else{
        if($result!="") {
            $arr[] = $result;
            $result="";
        }
    }
}

Print data:

  Array
  (
    [0] => 2019
    [1] => 11
    [2] => 18
    [3] => 17
    [4] => 25
  )

Recommended learning: "PHP Video Tutorial"

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

Statement:
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