How 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'

Source: Internet
Author: User
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"
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.