SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: ArrayArray

  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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";

    PHP Code:
    <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

    PHP Code:
    $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

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What you need is an array with all allowed colors.
    Then loop through that array with foreach:
    PHP Code:
    <?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
      
    }
    ?>

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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)

    PHP Code:
    $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

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Let's run through a cycle of your loop to see what it's doing.
    PHP Code:
    $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 foreach does.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi starlion

    sorry for asking it again

    PHP Code:
    $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 "
    PHP Code:
    $cl "red " 
    why

    vineet

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    because the code LOOPs.
    PHP Code:
    $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. 
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •