How to determine the type of the string's median and convert it to the corresponding variable type example 12.1, 10, string-& gt; 12.1, 10, how does string determine the type of the string's median and convert it to the corresponding variable type. For example, there is a variable $ aarray (12.1, 10, string); to convert it into an array (12.1, 10, string); what should I do? I used the eval function to determine the type of the string's median and convert it to the corresponding variable type. example '12. 1 ', '10', 'string'-> 12.1, 10, 'string'
How to determine the type of the string value and convert it to the corresponding variable type.
For example, a variable $ a = array ('12. 1 ', '10', 'string'); convert it to array (12.1, 10, 'string'); what should I do?
After I use the eval function, a warning will appear. although @ can be used to suppress it, it is not a good solution. do you want to use regular expressions?
The conversion type is used to insert data into the database. Is there any way for the database to convert this field? (my database knowledge is poor )?
You are the best expert.
The following is my code using the eval function.
PHP code
$a = array("12.1","10","sting");foreach ($a as $t){ eval("\$t = $t;"); var_dump($t); echo '
';}
------ Solution --------------------
I am also a beginner. based on what I just learned, I tried to compile a code segment to see if it is what you want:
$ A = array ("12", "10.8", "string ");
Foreach ($ a as $ key => $ value)
Echo $ value. ':'. gettype ($ value ).'
';
Function changetype (& $ value)
{
If (is_numeric ($ value )){
If (strstr ($ value ,'.'))
Settype ($ value, 'double ');
Else
Settype ($ value, 'integer ');
}
}
Array_walk ($ a, 'changetype ');
Foreach ($ a as $ key => $ value)
Echo $ value. ':'. gettype ($ value ).'
';
?>
The running result is as follows:
12: string
December 10.8: string
String: string
12: integer
December 10.8: double
String: string
――――――――――
------ Solution --------------------
/*** Assign the correct type to the input data **/
Function format (& $ v ){
If (is_array ($ v )){
$ V = array_map ('format', $ v );
}
If (is_numeric ($ v) $ v + = 0;
Return $ v;
}
$ A = array ("12.1", "10", "sting ");
Var_dump ($ );
Format ($ );
Var_dump ($ );
Before execution
Array (3 ){
[0] =>
String (4) 12.1"
[1] =>
String (2) "10"
[2] =>
String (5) "sting"
}
After Execution
Array (3 ){
[0] =>
Float (12.1)
[1] =>
Int (10)
[2] =>
String (5) "sting"
}