如何将字符串转换成PHP中的数字?

时间:2022-10-30 11:56:59

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP?

我想要将这些类型的值,3','2.34','0.234343'等转换成一个数字。在JavaScript中,我们可以使用Number(),但是PHP中有类似的方法吗?

Input             Output
'2'               2
'2.34'            2.34
'0.3454545'       0.3454545

28 个解决方案

#1


592  

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

您通常不需要这样做,因为PHP将在大多数情况下强制您使用该类型。对于需要显式转换类型的情况,请选择:

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

#2


105  

There are a few ways to do so:

有几种方法可以做到:

  1. Cast the strings to numeric primitive data types:

    将字符串转换为数字原始数据类型:

    $num = (int) "10";
    $num = (double) "10.12"; // same as (float) "10.12";
    
  2. Perform math operations on the strings:

    对字符串进行数学运算:

    $num = "10" + 1;
    $num = floor("10.1");
    
  3. Use intval() or floatval():

    使用intval中()或floatval():

    $num = intval("10");
    $num = floatval("10.1");
    
  4. Use settype().

    使用settype()。

#3


44  

In whatever (loosely-typed) language you can always cast a string to a number by adding a zero to it.

在任何(松散类型)语言中,都可以通过向数字添加一个0来将字符串转换为数字。

However, there is very little sense in this as PHP will do it automatically at the time of using this variable, and it will be cast to a string anyway at the time of output.

但是,在使用这个变量时,PHP将自动执行它,而且在输出时它将被转换成字符串。

Note that you may wish to keep dotted numbers as strings, because after casting to float it may be changed unpredictably, due to float numbers' nature.

注意,您可能希望将虚线数字保留为字符串,因为在强制转换为浮点数之后,由于浮点数的性质,可能会不可预测地更改它。

#4


28  

To avoid problems try intval($var). Some examples:

要避免问题,请尝试intval($var)。一些例子:

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34 (octal as starts with zero)
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26 (hex as starts with 0x)
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>

#5


23  

You can use:

您可以使用:

(int)(your value);

Or you can use:

或者你可以使用:

intval(string)

#6


17  

If you want get float for $value = '0.4', but int for $value = '4', you can write:

如果你想要$value = '0.4'的浮动,但是$value = '4'的int,你可以这样写:

$number = ($value == (int) $value) ? (int) $value : (float) $value;

Little bit dirty, but it works.

虽然有点脏,但很管用。

#7


14  

You can use (int)_value_ for example.

例如,可以使用(int)_value_。

$string = '100';
$int = (int)$string;

See Type Juggling.

看到类型杂耍。

#8


11  

In PHP you can use intval(string) or floatval(string) functions to convert strings to numbers.

在PHP中,可以使用intval(string)或floatval(string)函数将字符串转换为数字。

#9


10  

Just a little note to the answers that can be useful and safer in some cases. You may want to check if the string actually contains a valid numeric value first and only then convert it to a numeric type (for example if you have to manipulate data coming from a db that converts ints to strings). You can use is_numeric() and then floatval():

在某些情况下,只需要记下一些有用和安全的答案。您可能想先检查字符串是否包含有效的数值,然后才将其转换为数值类型(例如,如果您必须操作来自db的数据,该数据将int转换为字符串)。可以使用is_numeric(),然后使用floatval():

$a = "whatever"; // any variable

if (is_numeric($a)) 
    var_dump(floatval($a)); // type is float
else 
    var_dump($a); // any type

#10


9  

You can always add zero to it!

你可以给它加上零!

Input             Output
'2' + 0           2 (int)
'2.34' + 0        2.34 (float)
'0.3454545' + 0   0.3454545 (float)

#11


8  

Instead of having to choose weather to convert the string to int or float. You can simply add a 0 to it and PHP will automatically convert the result to a numeric type.

不需要选择weather来将字符串转换为int或float。您只需向它添加一个0,PHP将自动将结果转换为数字类型。

// being sure the string is actually a number
if( is_numeric($string) )
    $number = $string + 0;
else // let's the number be 0 if the string is not a number
    $number = 0;

#12


7  

I've found that in JavaScript a simple way to convert a string to a number is to multiply it by 1. It resolves the concatenation problem, because the "+" symbol has multiple uses in JavaScript, while the "*" symbol is purely for mathematical multiplication.

我发现在JavaScript中,将一个字符串转换成一个数字的简单方法是将它乘以1。它解决了连接问题,因为“+”符号在JavaScript中有多种用途,而“*”符号纯粹用于数学乘法。

Based on what I've seen here regarding PHP automatically being willing to interpret a digit-containing string as a number (and the comments about adding, since in PHP the "+" is purely for mathematical addition), this multiply trick works just fine for PHP, also.

基于我在这里看到的关于PHP自动愿意将包含数字的字符串解释为数字(以及关于添加的评论,因为在PHP中“+”纯粹是用于数学加法),这个乘法技巧对PHP也同样适用。

I have tested it, and it does work... Although depending on how you acquired the string, you might want to apply the trim() function to it, before multiplying by 1.

我测试过它,它确实有用……尽管根据您获得字符串的方式不同,您可能希望将trim()函数应用到它,然后再乘以1。

#13


7  

Here is a function I wrote to simplify things for myself:

我写了一个函数来简化我自己:

It also returns shorthand versions of boolean, integer, double and real.

它还返回boolean、integer、double和real的简写版本。

function type($mixed, $parseNumeric = false)
{        
    if ($parseNumeric && is_numeric($mixed)) {
        //Set type to relevant numeric format
        $mixed += 0;
    }
    $t = gettype($mixed);
    switch($t) {
        case 'boolean': return 'bool'; //shorthand
        case 'integer': return 'int';  //shorthand
        case 'double': case 'real': return 'float'; //equivalent for all intents and purposes
        default: return $t;
    }
}

Calling type with parseNumeric set to true will convert numeric strings before checking type.

将参数设置为true的调用类型将在检查类型之前转换数值字符串。

Thus:

因此:

type("5", true) will return int

类型(“5”,true)将返回int

type("3.7", true) will return float

类型(“3.7”,true)将返回float

type("500") will return string

类型(“500”)将返回字符串

Just be careful since this is a kind of false checking method and your actual variable will still be a string. You will need to convert the actual variable to the correct type if needed. I just needed it to check if the database should load an item id or alias, thus not having any unexpected effects since it will be parsed as string at run time anyway.

注意,这是一种错误的检查方法,实际的变量仍然是字符串。如果需要,您需要将实际的变量转换为正确的类型。我只是需要它检查数据库是否应该加载项id或别名,因此不会产生任何意外的影响,因为它将在运行时被解析为字符串。

Edit

编辑

If you would like to detect if objects are functions add this case to the switch:

如果您想要检测对象是否为函数,请将此案例添加到交换机:

case 'object': return is_callable($mixed)?'function':'object';

#14


5  

Here is the function that achieves what you are looking for. First we check if the value can be understood as a number, if so we turn it into an int and a float. If the int and float are the same (e.g., 5 == 5.0) then we return the int value. If the int and float are not the same (e.g., 5 != 5.3) then we assume you need the precision of the float and return that value. If the value isn't numeric we throw a warning and return null.

这就是你所追求的功能。首先,我们检查这个值是否可以理解为一个数字,如果可以,我们将它转换为int和float。如果int和float是相同的(例如,5 == 5.0),则返回int值。如果int和float不相同(例如,5 != 5.3),那么我们假设您需要float的精度并返回该值。如果值不是数值,我们抛出一个警告并返回null。

function toNumber($val) {
    if (is_numeric($val)) {
        $int = (int)$val;
        $float = (float)$val;

        $val = ($int == $float) ? $int : $float;
        return $val;
    } else {
        trigger_error("Cannot cast $val to a number", E_USER_WARNING);
        return null;
    }
}

#15


4  

$a = "10";

$b = (int)$a;

You can use this to convert a string to an int in PHP.

您可以使用它将字符串转换为PHP中的int类型。

#16


4  

You can use:

您可以使用:

((int) $var)   ( but in big number it return 2147483647 :-) )

But the best solution is to use:

但最好的解决办法是:

if (is_numeric($var))
    $var = (isset($var)) ? $var : 0;
else
    $var = 0;

Or

if (is_numeric($var))
    $var = (trim($var) == '') ? 0 : $var;
else
    $var = 0;

#17


4  

Try using something like this:

试着用以下方法:

<?php
$string='5';// $ string is a variable to hold string '5'
$int=intval($string);// $int is the converted string to integer  $int=intval('5');
echo$int;//5
?>

#18


4  

    $num = "3.14";
    $int = (int)$num; // output = 3
    $float = (float)$num; // output = 3.14
    $double = (double)$num; // output = 3.14

you can convert a string to a number in PHP using cast .

可以使用cast在PHP中将字符串转换为数字。

#19


4  

In addition to Boykodev answer I suggest this:

除了Boykodev的回答,我建议:

Input             Output
'2' * 1           2 (int)
'2.34' * 1        2.34 (float)
'0.3454545' * 1   0.3454545 (float)

#20


3  

You can change the data type as follows

您可以更改数据类型如下所示

$number = "1.234";

echo gettype ($number) . "\n"; //Returns string

settype($number , "float");

echo gettype ($number) . "\n"; //Returns float

For historical reasons "double" is returned in case of a float.

由于历史原因,在浮动时返回“double”。

PHP Documentation

PHP文档

#21


3  

For your specific case, you could just multiply by 1 to convert string to number in php. PHP takes care of the floating and integer automatically.

对于特定的情况,只需乘以1就可以将php中的字符串转换为数字。PHP自动处理浮动和整数。

$num = "2.12";
var_dump(is_float($num)); //bool(false)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(true)

$num = $num*1;
var_dump(is_float($num)); //bool(true)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(false)

#22


2  

PHP will do it for you within limits

PHP将在一定的范围内为您做这件事

<?php
   $str="3.148";
   $num=$str;

   printf("%f\n", $num);
?>

#23


2  

all suggestions loose the numeric type this seems to me a best practice

所有的建议都忽略了数字类型,在我看来这是一个最佳实践

function str2num($s){
//returns a num or FALSE
    $return_value =  !is_numeric($s) ? false :               (intval($s)==floatval($s)) ? intval($s) :floatval($s);
    print "\nret=$return_value type=".gettype($return_value)."\n";
}

#24


0  

I got the question "say you were writing the built in function for casting an integer to a string in PHP, how would you write that function" in a programming interview. Here's a solution.

我有个问题"假设你在写一个内建函数,用PHP将一个整数转换成一个字符串,你会怎么写这个函数"在一次编程面试中。这里有一个解决方案。

$nums = ["0","1","2","3","4","5","6","7","8","9"];
$int = 15939; 
$string = ""; 
while ($int) { 
    $string .= $nums[$int % 10]; 
    $int = (int)($int / 10); 
} 
$result = strrev($string);

#25


0  

you can simply use intval() function to convert string to int

您可以简单地使用intval()函数将字符串转换为int。

#26


0  

Yes, there is a similar method in PHP, but it is so little known that you will rarely hear about it. It is an arithmetic operators called "identity", as described here:

是的,PHP中也有类似的方法,但是我们对它所知甚少,所以您很少会听说它。它是一种叫做“同一性”的算术运算符,如下所述:

Aritmetic Operators

算术运算符

To convert a numeric string to a number, do as follows:

要将数字字符串转换为数字,请执行以下操作:

$a = +$a;

#27


-2  

$a = "7.2345";
$b = round(((float)$a),0);

#28


-4  

Different Approach:

不同的方法:

  1. push it all into an array / associative array.
  2. 将它们全部推入数组/关联数组。
  3. json_encode($your_array, JSON_NUMERIC_CHECK ); optionally decode it back
  4. json_encode()美元your_array JSON_NUMERIC_CHECK);选择解码回来
  5. ?
  6. 吗?
  7. profit!
  8. 利润!

#1


592  

You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, cast it:

您通常不需要这样做,因为PHP将在大多数情况下强制您使用该类型。对于需要显式转换类型的情况,请选择:

$num = "3.14";
$int = (int)$num;
$float = (float)$num;

#2


105  

There are a few ways to do so:

有几种方法可以做到:

  1. Cast the strings to numeric primitive data types:

    将字符串转换为数字原始数据类型:

    $num = (int) "10";
    $num = (double) "10.12"; // same as (float) "10.12";
    
  2. Perform math operations on the strings:

    对字符串进行数学运算:

    $num = "10" + 1;
    $num = floor("10.1");
    
  3. Use intval() or floatval():

    使用intval中()或floatval():

    $num = intval("10");
    $num = floatval("10.1");
    
  4. Use settype().

    使用settype()。

#3


44  

In whatever (loosely-typed) language you can always cast a string to a number by adding a zero to it.

在任何(松散类型)语言中,都可以通过向数字添加一个0来将字符串转换为数字。

However, there is very little sense in this as PHP will do it automatically at the time of using this variable, and it will be cast to a string anyway at the time of output.

但是,在使用这个变量时,PHP将自动执行它,而且在输出时它将被转换成字符串。

Note that you may wish to keep dotted numbers as strings, because after casting to float it may be changed unpredictably, due to float numbers' nature.

注意,您可能希望将虚线数字保留为字符串,因为在强制转换为浮点数之后,由于浮点数的性质,可能会不可预测地更改它。

#4


28  

To avoid problems try intval($var). Some examples:

要避免问题,请尝试intval($var)。一些例子:

<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34 (octal as starts with zero)
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26 (hex as starts with 0x)
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>

#5


23  

You can use:

您可以使用:

(int)(your value);

Or you can use:

或者你可以使用:

intval(string)

#6


17  

If you want get float for $value = '0.4', but int for $value = '4', you can write:

如果你想要$value = '0.4'的浮动,但是$value = '4'的int,你可以这样写:

$number = ($value == (int) $value) ? (int) $value : (float) $value;

Little bit dirty, but it works.

虽然有点脏,但很管用。

#7


14  

You can use (int)_value_ for example.

例如,可以使用(int)_value_。

$string = '100';
$int = (int)$string;

See Type Juggling.

看到类型杂耍。

#8


11  

In PHP you can use intval(string) or floatval(string) functions to convert strings to numbers.

在PHP中,可以使用intval(string)或floatval(string)函数将字符串转换为数字。

#9


10  

Just a little note to the answers that can be useful and safer in some cases. You may want to check if the string actually contains a valid numeric value first and only then convert it to a numeric type (for example if you have to manipulate data coming from a db that converts ints to strings). You can use is_numeric() and then floatval():

在某些情况下,只需要记下一些有用和安全的答案。您可能想先检查字符串是否包含有效的数值,然后才将其转换为数值类型(例如,如果您必须操作来自db的数据,该数据将int转换为字符串)。可以使用is_numeric(),然后使用floatval():

$a = "whatever"; // any variable

if (is_numeric($a)) 
    var_dump(floatval($a)); // type is float
else 
    var_dump($a); // any type

#10


9  

You can always add zero to it!

你可以给它加上零!

Input             Output
'2' + 0           2 (int)
'2.34' + 0        2.34 (float)
'0.3454545' + 0   0.3454545 (float)

#11


8  

Instead of having to choose weather to convert the string to int or float. You can simply add a 0 to it and PHP will automatically convert the result to a numeric type.

不需要选择weather来将字符串转换为int或float。您只需向它添加一个0,PHP将自动将结果转换为数字类型。

// being sure the string is actually a number
if( is_numeric($string) )
    $number = $string + 0;
else // let's the number be 0 if the string is not a number
    $number = 0;

#12


7  

I've found that in JavaScript a simple way to convert a string to a number is to multiply it by 1. It resolves the concatenation problem, because the "+" symbol has multiple uses in JavaScript, while the "*" symbol is purely for mathematical multiplication.

我发现在JavaScript中,将一个字符串转换成一个数字的简单方法是将它乘以1。它解决了连接问题,因为“+”符号在JavaScript中有多种用途,而“*”符号纯粹用于数学乘法。

Based on what I've seen here regarding PHP automatically being willing to interpret a digit-containing string as a number (and the comments about adding, since in PHP the "+" is purely for mathematical addition), this multiply trick works just fine for PHP, also.

基于我在这里看到的关于PHP自动愿意将包含数字的字符串解释为数字(以及关于添加的评论,因为在PHP中“+”纯粹是用于数学加法),这个乘法技巧对PHP也同样适用。

I have tested it, and it does work... Although depending on how you acquired the string, you might want to apply the trim() function to it, before multiplying by 1.

我测试过它,它确实有用……尽管根据您获得字符串的方式不同,您可能希望将trim()函数应用到它,然后再乘以1。

#13


7  

Here is a function I wrote to simplify things for myself:

我写了一个函数来简化我自己:

It also returns shorthand versions of boolean, integer, double and real.

它还返回boolean、integer、double和real的简写版本。

function type($mixed, $parseNumeric = false)
{        
    if ($parseNumeric && is_numeric($mixed)) {
        //Set type to relevant numeric format
        $mixed += 0;
    }
    $t = gettype($mixed);
    switch($t) {
        case 'boolean': return 'bool'; //shorthand
        case 'integer': return 'int';  //shorthand
        case 'double': case 'real': return 'float'; //equivalent for all intents and purposes
        default: return $t;
    }
}

Calling type with parseNumeric set to true will convert numeric strings before checking type.

将参数设置为true的调用类型将在检查类型之前转换数值字符串。

Thus:

因此:

type("5", true) will return int

类型(“5”,true)将返回int

type("3.7", true) will return float

类型(“3.7”,true)将返回float

type("500") will return string

类型(“500”)将返回字符串

Just be careful since this is a kind of false checking method and your actual variable will still be a string. You will need to convert the actual variable to the correct type if needed. I just needed it to check if the database should load an item id or alias, thus not having any unexpected effects since it will be parsed as string at run time anyway.

注意,这是一种错误的检查方法,实际的变量仍然是字符串。如果需要,您需要将实际的变量转换为正确的类型。我只是需要它检查数据库是否应该加载项id或别名,因此不会产生任何意外的影响,因为它将在运行时被解析为字符串。

Edit

编辑

If you would like to detect if objects are functions add this case to the switch:

如果您想要检测对象是否为函数,请将此案例添加到交换机:

case 'object': return is_callable($mixed)?'function':'object';

#14


5  

Here is the function that achieves what you are looking for. First we check if the value can be understood as a number, if so we turn it into an int and a float. If the int and float are the same (e.g., 5 == 5.0) then we return the int value. If the int and float are not the same (e.g., 5 != 5.3) then we assume you need the precision of the float and return that value. If the value isn't numeric we throw a warning and return null.

这就是你所追求的功能。首先,我们检查这个值是否可以理解为一个数字,如果可以,我们将它转换为int和float。如果int和float是相同的(例如,5 == 5.0),则返回int值。如果int和float不相同(例如,5 != 5.3),那么我们假设您需要float的精度并返回该值。如果值不是数值,我们抛出一个警告并返回null。

function toNumber($val) {
    if (is_numeric($val)) {
        $int = (int)$val;
        $float = (float)$val;

        $val = ($int == $float) ? $int : $float;
        return $val;
    } else {
        trigger_error("Cannot cast $val to a number", E_USER_WARNING);
        return null;
    }
}

#15


4  

$a = "10";

$b = (int)$a;

You can use this to convert a string to an int in PHP.

您可以使用它将字符串转换为PHP中的int类型。

#16


4  

You can use:

您可以使用:

((int) $var)   ( but in big number it return 2147483647 :-) )

But the best solution is to use:

但最好的解决办法是:

if (is_numeric($var))
    $var = (isset($var)) ? $var : 0;
else
    $var = 0;

Or

if (is_numeric($var))
    $var = (trim($var) == '') ? 0 : $var;
else
    $var = 0;

#17


4  

Try using something like this:

试着用以下方法:

<?php
$string='5';// $ string is a variable to hold string '5'
$int=intval($string);// $int is the converted string to integer  $int=intval('5');
echo$int;//5
?>

#18


4  

    $num = "3.14";
    $int = (int)$num; // output = 3
    $float = (float)$num; // output = 3.14
    $double = (double)$num; // output = 3.14

you can convert a string to a number in PHP using cast .

可以使用cast在PHP中将字符串转换为数字。

#19


4  

In addition to Boykodev answer I suggest this:

除了Boykodev的回答,我建议:

Input             Output
'2' * 1           2 (int)
'2.34' * 1        2.34 (float)
'0.3454545' * 1   0.3454545 (float)

#20


3  

You can change the data type as follows

您可以更改数据类型如下所示

$number = "1.234";

echo gettype ($number) . "\n"; //Returns string

settype($number , "float");

echo gettype ($number) . "\n"; //Returns float

For historical reasons "double" is returned in case of a float.

由于历史原因,在浮动时返回“double”。

PHP Documentation

PHP文档

#21


3  

For your specific case, you could just multiply by 1 to convert string to number in php. PHP takes care of the floating and integer automatically.

对于特定的情况,只需乘以1就可以将php中的字符串转换为数字。PHP自动处理浮动和整数。

$num = "2.12";
var_dump(is_float($num)); //bool(false)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(true)

$num = $num*1;
var_dump(is_float($num)); //bool(true)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(false)

#22


2  

PHP will do it for you within limits

PHP将在一定的范围内为您做这件事

<?php
   $str="3.148";
   $num=$str;

   printf("%f\n", $num);
?>

#23


2  

all suggestions loose the numeric type this seems to me a best practice

所有的建议都忽略了数字类型,在我看来这是一个最佳实践

function str2num($s){
//returns a num or FALSE
    $return_value =  !is_numeric($s) ? false :               (intval($s)==floatval($s)) ? intval($s) :floatval($s);
    print "\nret=$return_value type=".gettype($return_value)."\n";
}

#24


0  

I got the question "say you were writing the built in function for casting an integer to a string in PHP, how would you write that function" in a programming interview. Here's a solution.

我有个问题"假设你在写一个内建函数,用PHP将一个整数转换成一个字符串,你会怎么写这个函数"在一次编程面试中。这里有一个解决方案。

$nums = ["0","1","2","3","4","5","6","7","8","9"];
$int = 15939; 
$string = ""; 
while ($int) { 
    $string .= $nums[$int % 10]; 
    $int = (int)($int / 10); 
} 
$result = strrev($string);

#25


0  

you can simply use intval() function to convert string to int

您可以简单地使用intval()函数将字符串转换为int。

#26


0  

Yes, there is a similar method in PHP, but it is so little known that you will rarely hear about it. It is an arithmetic operators called "identity", as described here:

是的,PHP中也有类似的方法,但是我们对它所知甚少,所以您很少会听说它。它是一种叫做“同一性”的算术运算符,如下所述:

Aritmetic Operators

算术运算符

To convert a numeric string to a number, do as follows:

要将数字字符串转换为数字,请执行以下操作:

$a = +$a;

#27


-2  

$a = "7.2345";
$b = round(((float)$a),0);

#28


-4  

Different Approach:

不同的方法:

  1. push it all into an array / associative array.
  2. 将它们全部推入数组/关联数组。
  3. json_encode($your_array, JSON_NUMERIC_CHECK ); optionally decode it back
  4. json_encode()美元your_array JSON_NUMERIC_CHECK);选择解码回来
  5. ?
  6. 吗?
  7. profit!
  8. 利润!