ArrayArray

hi all

i m having multiple choices of colours with checkbox for each color.

i want to check if chosen color value exists in “array” then echo “checked”;


<input type="checkbox" name="colors[]" id="colors[]" value="red" <? if(in_array('red', $colors)) echo "checked='checked'"; ?> />

This code works fine but at present i have to change the color value in the “in_array” code manually for each color.

so i thought of creating a function which will automatic check for color value existance and then will echo “checked”.
but i am stuck in this below function


$chosencolors=array("red","yellow");
foreach($chosencolors as $colr)
{
	$cl = $colr ." "; // output as red,yellow
	$clr = explode(" ",$cl);
	echo $clr; // output as ArrayArray
	echo $clr[0]; // output as redyellow
}

why is $clr giving output as ArrayArray. Its not splitting with explode.

is there any other way of doing the same task.

vineet

What you need is an array with all allowed colors.
Then loop through that array with foreach:


<?php
  $allowedColors = array("red", "yellow");
  foreach ($allowedColors as $allowedColor) {
?>
    <input type="checkbox" name="colors[]" id="colors[]" value="<?php echo $allowedColor; ?>" <?php if(in_array($allowedColor, $colors)) echo "checked='checked'"; ?> />
<?php
  }
?>

hi guido

thanks for the solution.

but i would like to know why is in my code “explode function” not splitting text.

Its output result as ArrayArray or redyellow (see the comments)


$chosencolors=array("red","yellow");
foreach($chosencolors as $colr)
{
    $cl = $colr ." "; // output as red,yellow
    $clr = explode(" ",$cl);
    echo $clr; // output as ArrayArray
    echo $clr[0]; // output as redyellow
}  

vineet

Let’s run through a cycle of your loop to see what it’s doing.


$chosencolors=array("red","yellow"); // Defined array.
foreach($chosencolors as $colr) //$colr = "red"
{
    $cl = $colr ." "; // $cl = "red "
    $clr = explode(" ",$cl); // $clr = array(0 => "red", 1 => "");
    echo $clr; // echoes Array, because $clr is an array, and Array->toString = "Array".
    echo $clr[0]; // echoes "red"
}  

Now repeat the loop, except wherever the word “red” is, replace it with yellow.
I think perhaps you’ve misunderstood what [FPHP]foreach[/FPHP] does.

hi starlion

sorry for asking it again


$chosencolors=array("red","yellow");
foreach($chosencolors as $colr)
{
    $cl = $colr ." "; 
    echo $cl; // output as red yellow
}

$cl gives me output as “red yellow”.

But according to you $cl= "red "


$cl = "red " 

why

vineet

because the code LOOPs.


$chosencolors=array("red","yellow");
foreach($chosencolors as $colr) //Start loop. Get first element of array (red), store it in $colr.
{
    $cl = $colr ." "; 
    echo $cl; // First time through this will echo "red ", second time through it will echo "yellow "
}  //Go back to top of loop, use next value of array for $colr. If there are no more elements in the array, exit loop.

thanks starlion for explaining it.

so it means “explode” function will never work with/ inside foreach loop like as i was trying to do.

vineet