Why is my PHP multi dimensional array not working?

前端 未结 4 1942
萌比男神i
萌比男神i 2021-01-16 12:26

My multi-dimensional array is working. But I cannot seem to use explode or in_array to limit the array when calling via $_GET

相关标签:
4条回答
  • 2021-01-16 12:57

    Your col-Variable contains a number (1, 2 or 3) But the explode-call returns an Array containing words (green, blue, orange)

    Even though PHP could handle the cast from 1 to "1", it can not cast from 1 to "green".

    0 讨论(0)
  • 2021-01-16 12:57

    I think you can simply do something like this:

    <?
    $shop = array(
    array("red", "black", "blue", "green"),
    array("orange"),
    array("orange", "black"),
    array("pink", "yellow")
    );
    
    foreach ($shop as $rowNumber => $row)
    {
        echo "<li><b>The row number $rowNumber</b>";
        echo "<ul>";
        foreach ($row as $col)
        {
            //Compare both values, if they match, it prints
            if ($col == $_GET['filter'])
            {
                 echo "<li>".$col."</li>";
            }
        }
        echo "</ul>";
        echo "</li>";
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-16 12:59

    Updated

    Solution 1

       <?
        
        $shop = array(
        array("1", "red", "black", "blue and green"),
        array("2", "orange"),
        array("3", "pink", "yellow", "blue and green")
        );
        
        for ($row = 0; $row < count($shop); $row++)    
         {
    
                $lis = "";
                for ($col = 0; $col < count($shop[$row]); $col++)
                    {
                         if (in_array($shop[$row][$col], explode(' and ', $_GET['filter'])) 
                            || empty($_GET['filter'])){
                            $lis .= "<li>".$col."</li>";
                         }
                    }
               if($lis != "") {
                 echo "<li><b>The row number $row</b>";
                 echo "<ul>";
                 echo $lis;
                 echo "</ul>";
                 echo "</li>";
             }
        }
        
        ?>
    

    Solution 2

    <?
    
    $shop = array(
    array("1", "red", "black", "blue and green"),
    array("2", "orange"),
    array("3", "pink", "yellow", "blue and green")
    );
    
    for ($row = 0; $row < count($shop); $row++)    
    {
        $lis = "";
        
        for ($col = 0; $col < count($shop[$row]); $col++)
        {
                if (in_array($shop[$row][$col], explode(' and ', $_GET['filter']))){
                    $lis .= "<li>".$col."</li>";
                }
        }
        
        if($lis=="") {
            
                echo "$row";
            
        } else {
            
                echo $lis;
            
        }
    }
    
    ?>
    
    0 讨论(0)
  • 2021-01-16 13:13

    Just Gussing maybe you want to print somthing like this

    $shop = array(array("1","red","black","blue and green"),array("2","orange"),array("3","pink","yellow","blue and green"));
    
    echo "<ul>";
    foreach ( $shop as $info ) {
        $info = array_pad($info, 4, "none");
        list($id, $color1, $color2, $mixed) = $info;
        printf("<li><b>The row number = %d , Color = 1  %s , Color 2 =   %s , Mixed  = %s </b></li>", $id, $color1, $color2, $mixed);
    }
    echo "</ul>";
    

    Output

    • The row number = 1 , Color = 1 red , Color 2 = black , Mixed = blue and green
    • The row number = 2 , Color = 1 orange , Color 2 = none , Mixed = none
    • The row number = 3 , Color = 1 pink , Color 2 = yellow , Mixed = blue and green
    0 讨论(0)
提交回复
热议问题