1

我的多维数组正在工作。但是通过调用时我似乎无法使用explodein_array限制数组$_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>";
}

?>

如果我运行脚本,$_GET["filter"]=="black"它会显示所有项目 - 它应该只显示两个,例如这是错误的:其他行不应该出现:

在此处输入图像描述

应该是这个

在此处输入图像描述

4

4 回答 4

2

更新

解决方案 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>";
         }
    }
    
    ?>

解决方案 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;
        
    }
}

?>
于 2012-10-01T11:43:29.607 回答
0

您的 col-Variable 包含一个数字(1、2 或 3)但爆炸调用返回一个包含单词(绿色、蓝色、橙色)的数组

尽管 PHP 可以处理从 1 到 "1" 的转换,但它不能从 1 转换到 "green"。

于 2012-10-01T11:42:22.330 回答
0

Just Gussing 也许你想打印这样的东西

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

输出

  • 行号 = 1 ,颜色 = 1 红色 ,颜色 2 = 黑色 ,混合 = 蓝色和绿色
  • 行号 = 2 ,颜色 = 1 橙色 ,颜色 2 = 无 ,混合 = 无
  • 行号 = 3 ,颜色 = 1 粉色 ,颜色 2 = 黄色 ,混合 = 蓝色和绿色
于 2012-10-01T11:43:09.493 回答
0

我认为你可以简单地做这样的事情:

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

?>
于 2012-10-01T12:00:58.957 回答