简体   繁体   中英

How to type cast variables inside a foreach loop in PHP?

I'm looping through an array using a foreach loop:

$attributes = array( 'drink', 'price', 'amount' );

foreach ( $attributes as $key ) {
    $info[$key] = $key;
}

How can I type cast my $key variable differently each time the loop runs? For example, I need drink to be type string , price to be float and amount to be int .

Please note, this is a simplified example of my code. Type casting the keys before the loop is run isn't suitable in my case.

I tried the following but it didn't seem to work:

$attributes = array( 'drink' => 'string', 'price' => 'float', 'amount' => 'int' );

foreach ( $attributes as $key => $value ) {
    $info[$key] = ($value) $key;
}

This should work for you:

(Here I use the key to then call either strval() , floatval() or intval() )

$attributes = array("str" => 'drink',"float"=> "10.5","int"=> "10" );
foreach ( $attributes as $k => $key ) {
    $k.="val";
    $info[$key] = $k($key);
}

var_dump($info);

output:

array(3) {
  ["drink"]=>
  string(5) "drink"
  ["10.5"]=>
  float(10.5)
  [10]=>
  int(10)
}

Use an assoc array and use the key as the type cast. For example

$attributes = array("string" => array("drink"), 
                    "float" => array("price"), 
                    "int" => array("amount"));

$array = array();
foreach($attributes as $cast => $val) {
   switch($cast) {
      case 'int' :
        $array[] = (int) $val;
        break;
      case 'float' :
        $array[] = (float) $val;
        break;
      case 'string' :
      default :
        $array[] = (string) $val;
        break;
   }
}

https://eval.in/312808

Edit

If there are multiple elements needed to type cast, store them in an array (like above), and call array_map . For example:

$attributes = array("string" => array("drink"), 
                    "float" => array("price"), 
                    "int" => array("amount"));

$array = array();
foreach($attributes as $cast => $val) {
   switch($cast) {
      case 'int' :
        $array[] = array_map("intval",$val);
        break;
      case 'float' :
        $array[] = array_map("floatval",$val);
        break;
      case 'string' :
      default :
        $array[] = array_map("strval",$val);
        break;
   }
}

https://eval.in/312812

You can also use the settype() function, here adapted to your example:

$attributes = array( 'drink' => 'string', 'price' => 'float', 'amount' => 'int' );

foreach ( $attributes as $key => $value ) {
    settype($key, $value);
    $info[$key] = $key;
}

If the order in $attributes is constant you can try:

$attributes = array( 'drink', 'price', 'amount' );

foreach ( $attributes as list($drink, $price, $amount) ) {
    $info['drink'] = $drink;
    $info['price'] = (float) $price;
    $info['amount'] = (int) $amount;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM