My multi-dimensional array is working. But I cannot seem to use explode or in_array to limit the array when calling via $_GET
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".
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>";
}
?>
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;
}
}
?>
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