For a webstore script I am trying to create a filter for my two dimensional array. I have used a few guides and other answers here on Stackoverflow or w3Schools, but I keep running into the same problem;
The newly created array just has empty gaps where the filter takes out the data, rather than creating a 0 to 100 array full of relevant information.
Since I am still new to the whole multidimensional array thing, I was hoping somebody here could point me in the right direction and tell me what I`m doing wrong.
Array getting fully filled with stuff from my Database
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$product[] = [
"ID" => $row["ID"],
"Name" => $row["Productname"],
"Price" => $row["Pricetag"],
"Supply" => $row["Productsupply"],
"Type" => $row["Producttype"],
"Tags" => $row["Tags"],
"Materials" => $row["Materials"],
"Date" => $row["Releasedate"],
"Description" => $row["Description"],
"isDigital" => $row["isDigital"],
"MainPhoto" => $row["MainPhoto"],
"Photo01" => $row["Productpic01"],
"Photo02" => $row["Productpic02"],
"Photo03" => $row["Productpic03"]
];
}
} else {
echo "0 results found. Please check database connection.";
}
Script to filter this data and creating a new array based on the matches in the key:
$Filterarray = array('Pluimstaart'); //Might filter on more keywords in future usage.
$newArr = array();
foreach ($product as $key => $value) {
foreach ($value as $finalVal) {
if(in_array($finalVal, $Filterarray)){ // check if available in filter array
$newArr[$key]["ID"] = $value["ID"];
$newArr[$key]["Name"] = $value["Name"];
$newArr[$key]["Price"] = $value["Price"];
$newArr[$key]["Supply"] = $value["Supply"];
$newArr[$key]["Type"] = $value["Type"];
//$newArr[$key][] = $finalVal;
}
}
}
Output on website:
Array
(
[1] => Array
(
[ID] => 1
[Name] => Pluisje de Pluimstaart
[Price] => 75
[Supply] => 0
[Type] => Pluimstaart
)
[5] => Array
(
[ID] => 69
[Name] => Blizzard de Pluimstaart
[Price] => 75
[Supply] => 1
[Type] => Pluimstaart
)
)
The problem here being the skip from 1 to 5, I want the new array to just say
Array
(
[0] => Array
(
[ID] => 1
[Name] => Pluisje de Pluimstaart
[Price] => 75
[Supply] => 0
[Type] => Pluimstaart
)
[1] => Array
(
[ID] => 69
[Name] => Blizzard de Pluimstaart
[Price] => 75
[Supply] => 1
[Type] => Pluimstaart
)
)