Home  >  Article  >  Backend Development  >  How to merge arrays and convert them into numeric types in php

How to merge arrays and convert them into numeric types in php

青灯夜游
青灯夜游Original
2022-06-01 16:01:552192browse

Method: 1. Use "array_merge (array 1, array 2...)" to merge one or more arrays into one array; 2. Use "implode (merge array)" to convert the merged array into String; 3. Use "settype (string, type identifier)" to convert the string to the specified numerical type (integer or floating point number).

How to merge arrays and convert them into numeric types in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use array_merge () merge the arrays, and then convert the array type to a numeric type.

Step 1: Use array_merge() to merge arrays

The array_merge() function is used to merge one or more arrays into one array.

Note: If two or more array elements have the same key name, the last element will overwrite the other elements.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a1=array(1,2,3,4,5,0);
$a2=array(6,7,8,9,10);
$arr=array_merge($a1,$a2);
var_dump($arr);
?>

How to merge arrays and convert them into numeric types in php

Step 2: Use the implode() function to convert the merged array into a string

implode() function can convert a Convert one-dimensional array to string

$str=implode($arr);
var_dump($str);

How to merge arrays and convert them into numeric types in php

Step 3: Use settype() to convert the string to numeric type

settype ($var,$type)Used to set the variable $var to the specified $type type.

The numeric type can be set when $type is set to the following characters

  • "integer" (or "int", starting from PHP 4.2.0)

  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)

settype($str,"int");
var_dump($str);

settype($str,"float");
var_dump($str);

How to merge arrays and convert them into numeric types in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to merge arrays and convert them into numeric types 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