5

如果它们存在,我如何解析数组中的true和字符串以变为布尔值?false

例如,

形式

$config = array(
    "allow_n" => "true",
    "allow_m" => "false",
    "say"     => "hello"
);

$config = array(
    "allow_n" => true,
    "allow_m" => false,
    "say"     => "hello"
);

是否可以?

编辑:

谢谢你们的帮助。

抱歉,我忘了从一开始就澄清 - 例如,这种情况可能发生在多维数组中,

$config = array(
    "allow_n" => "true",
    "allow_m" => "false",
    "say"     => "Hello",
    "php"   => array(
        "oop" => "true",
        "classic" => "false"
    )
);
4

6 回答 6

5

你可以array_walk_recursive用来实现这一点:

例子

$config = array (
        "allow_n" => "true",
        "allow_m" => "false",
        "say" => "Hello",
        "php" => array (
                "oop" => "true",
                "classic" => "false" 
        ) 
);
var_dump ( $config );

array_walk_recursive ( $config, function (&$item) {
    if ($item == "true") {
        $item = true;
    } else if ($item == "false") {
        $item = false;
    } else if (is_numeric ( $item )) {
        $item = intval ( $item );
    }
} );

var_dump ( $config );

输出之前

'allow_n' => string 'true' (length=4)
  'allow_m' => string 'false' (length=5)
  'say' => string 'Hello' (length=5)
  'php' => 
    array
      'oop' => string 'true' (length=4)
      'classic' => string 'false' (length=5)

输出后

    array
  'allow_n' => boolean true
  'allow_m' => boolean false
  'say' => string 'Hello' (length=5)
  'php' => 
    array
      'oop' => boolean true
      'classic' => boolean false
于 2012-05-03T19:24:30.640 回答
4
foreach ($config as $k=>$v)
{
  $low_v = strtolower($v);
  if ($low_v == 'true')
    $config[$k] = true;
  else if ($low_v == 'false')
    $config[$k] = false; 
}
于 2012-05-03T19:21:28.810 回答
2

这是一个可以做到的循环

foreach ($config as $key => $val) {
    if ($val == 'true') {
        $config[$key] = true;
    } elseif ($val == 'false') {
        $config[$key] = false;
    }
}

编辑:这是一个递归和处理多维数组的函数

function tfSwap($arr) {
    foreach ($arr as $key => $val) {
        if (is_array($val)) {
            $arr[$key] = tfSwap($val);
        } else {
            if ($val == 'true') {
                $arr[$key] = true;
            } elseif ($val == 'false') {
                $arr[$key] = false;
            }
        }
    }  
    return $arr;
}

然后你可以这样称呼它:

$config = tfSwap($config);
于 2012-05-03T19:21:30.893 回答
2

我确定还有其他解决方案,但是您应该能够使用该filter_var功能并传入FILTER_VALIDATE_BOOLEAN标志。这会给你一个单行而不是写出一个循环。:)

filter_var($config, FILTER_VALIDATE_BOOLEAN);

// 编辑:与此处建议的相同:如何在 PHP 中解析布尔逻辑字符串

于 2012-05-03T19:24:48.730 回答
2

您还可以维护一个转换数组,并在遍历数组时查看那里。

$map = array("true" => true, "false"  => false);
foreach ($config as $key => $value) {
    if (array_key_exists($value, $map)) {
        $config[$key] = $map[$value];
    }
}
于 2012-05-03T19:34:22.920 回答
1

我的方法是:

function boolify( $var ) {
    if( is_array( $var ) ) foreach( $var as $ek=>$ev ) $ret[$ek] = boolify( $ev );
    else if( $var === 'false' ) $ret = false;
    else if( $var === 'true' ) $ret = true;         
    else $ret = $var;
    return $ret;
}

$config = array(
    "allow_n" => "true",
    "allow_m" => "false",
    "say"     => "Hello",
    "php"   => array(
        "oop" => "true",
        "classic" => "false"
    )
);

$config = boolify( $config ); 

var_dump( $config );

// output: array(4) { ["allow_n"]=> bool(true) ["allow_m"]=> bool(false) ["say"]=> string(5) "Hello" ["php"]=> array(2) { ["oop"]=> bool(true) ["classic"]=> bool(false) } } 
于 2012-05-03T20:38:00.473 回答