如何使用下拉菜单对数组列表进行排序以显示在页面上



我希望一个下拉菜单能够按下拉菜单中选择的选项将我存储在数组中的项目排序到页面上。

例如,如果我从下拉菜单中选择nike,则只有品牌名称为 nike 的数组中的项目将显示在页面上。

<?php
 //the data file
 $inventory = [];
 //shoes
 // steph curry
 $inventory[100] = [
  "name"        => "Steph's Clutchfit Drive",
  "item"        => "Shoes",
  "img"         => "images/shoes/stephcurry.png",
  "brand"       => "Under Armour",
  "color"       => "White , Blue , Yellow",
  "category"    => "Basketball",
  "type"        => "High",
  "size"        => "10 , 12 , 14",
  "price"       => "$ 80.00"
];
//micheal jordan
$inventory[101] = [
 "name"         => "Jordan 11's",
 "item"         => "Shoes",
 "img"          => "images/shoes/jordan11.png",
 "brand"        => "Nike",
 "color"        => "White , Blue , Red",
 "category" => "Basketball",
 "type"     => "High",
 "size"     => "9 , 11 , 12",
 "price"        => "$ 110.00"
];

网页文件

当我选择耐克时,我只想要上面数组列表中的项目,耐克显示在页面上。

<select>            
  <option value="nike">nike</option>
  <option value="reebok"reebok</option>
  <option value="underArmor">UA</option>
</select>

array_search和array_combine的组合可以帮助在 多维数组。

如果您的值中有id(数组索引(,并且只想获取一个耐克项目,那么这将起作用:

echo array_search("Nike", array_column($inventory, "brand", "id"));

如果您没有 id 列并且可以找到多个耐克商品,那么这将起作用:

print_r(array_keys(array_combine(array_keys($inventory), array_column($inventory, "brand")), "Nike"));

试着让我知道。

最新更新