How to parse true and false strings in an array to become boolean

How to parse a string trueand falsean array so that they became the logical, if they exist?

For instance,

form

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

to

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

Is it possible?

EDIT:

Thanks guys for the help.

Sorry, I forgot to clarify from the very beginning - this case can happen in a multi-level array, for example,

$config = array(
    "allow_n" => "true",
    "allow_m" => "false",
    "say"     => "Hello",
    "php"   => array(
        "oop" => "true",
        "classic" => "false"
    )
);
+5
source share
6 answers

you can use array_walk_recursiveto achieve this:

Example

$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 );

Exit to

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

Exit after

    array
  'allow_n' => boolean true
  'allow_m' => boolean false
  'say' => string 'Hello' (length=5)
  'php' => 
    array
      'oop' => boolean true
      'classic' => boolean false
+5
source
foreach ($config as $k=>$v)
{
  $low_v = strtolower($v);
  if ($low_v == 'true')
    $config[$k] = true;
  else if ($low_v == 'false')
    $config[$k] = false; 
}
+4
source

,

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);
+2

, , filter_var FILTER_VALIDATE_BOOLEAN. .:)

filter_var($config, FILTER_VALIDATE_BOOLEAN);

// edit: the same as suggested here: How to parse a string of logical logic in PHP

+1
source

You can also maintain an array of transformations and look there, iterating over the array.

$map = array("true" => true, "false"  => false);
foreach ($config as $key => $value) {
    if (array_key_exists($value, $map)) {
        $config[$key] = $map[$value];
    }
}
+1
source

my approach would look like this:

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) } } 
0
source

All Articles