dongpao5658
dongpao5658
2015-04-13 08:31

如何在PHP中的foreach循环中键入转换变量?

已采纳

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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douzi1986 douzi1986 6年前

    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)
    }
    
    点赞 评论 复制链接分享
  • douchushao7799 douchushao7799 4年前

    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;
    }
    
    点赞 评论 复制链接分享
  • dongshen6060 dongshen6060 6年前

    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;
    }
    
    点赞 评论 复制链接分享
  • dtv55860 dtv55860 6年前

    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

    点赞 评论 复制链接分享

相关推荐