true is getting changed to 1
false is getting changed to 0

I can't just compare numbers, because some of the items in the array are numbers. I'd rather not look for string values of 'true' and 'false', because I have other code that needs to use the triple equal signs === so that things work properly.

What is the deal?
here is some code

$fields = array(
	array(
		'formtype' => 'input',
		'required' => true,
		'maxlength' => 10
	),
	array(
		'formtype' => 'input',
		'required' => false,
		'maxlength' => 2
	));

$total_fields = count($fields);
for ($x = 0; $x < $total_fields; $x++)
{
	//output inner arrays
	foreach ($fields[$x] as $key => $value)
	{
		echo 'key=' . $key . ' | value=' . $value . '<hr>';
	}
}

    Is this what you're looking for:

    $value = (is_bool($value)) ? (($value) ? 'true' : 'false') : $value;
    echo 'key=' . $key . ' | value=' . $value . '<hr>';
     

      misread first time, but i don't see why you cant just use "===" for the comparison not "==" then you get the type compared as well

        PHP is not changing anything in the array - it's changing what you're telling it to output. [man]echo[/man] only outputs strings, so if you try to concatenate a boolean into a string, PHP automatically converts the boolean type into a string-equivalent so that it can join the like object types together. For PHP, boolean TRUE is converted to '1' when cast to a string (instead of the string 'true').

        If you don't believe me, use a function like [man]print_r/man or, even better, [man]var_dump/man on the array $fields instead of your for() loop.

          Write a Reply...