Why is my PHP multi dimensional array not working?

随声附和 提交于 2019-12-01 10:29:06

问题


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

<?
$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) {
            if (in_array($col, explode(' and ', $_GET['filter']))){
                echo "<li>".$col."</li>";
            }
        }
        echo "</ul>";
    echo "</li>";
}

?>

If I run the script with $_GET["filter"]=="black" it displays all items - it should only display two, example this is wrong: the other rows should not appear:

should be this instead


回答1:


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;

    }
}

?>



回答2:


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




回答3:


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



回答4:


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

?>


来源:https://stackoverflow.com/questions/12672475/why-is-my-php-multi-dimensional-array-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!