我试图根据这个SO问题中找到的答案来实现这一点:How to search by key=>value in a multidimensional array in PHP - 但我没有运气。
我正在查询数据库。我需要把结果放在所有那些有'类型' = $键入一个新数组以使用该新数组显示信息。
搜索功能:
function search($array, $key, $value){
$results = array();
search_r($array, $key, $value, $results);
return $results;
}
function search_r($array, $key, $value, &$results){
if (!is_array($array)) {
return;
}
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
search_r($subarray, $key, $value, $results);
}
}
守则:
$getTypes = mysqli_query($link, "SELECT * FROM Type WHERE section = ".$selectValue." ORDER BY `order`");
$getCategories = mysqli_query($link, "SELECT * FROM Category WHERE section = ".$selectValue." ORDER BY `order`");
$getdbvalues = mysqli_query($link, "SELECT * FROM `Data` WHERE section = ".$selectValue." AND `date` = ".$datecomp);
$dbvalues = mysqli_fetch_assoc($getdbvalues);
while($type = mysqli_fetch_assoc($getTypes)){
$typeid = $type['id'];
$getdbdata = search($dbvalues, 'type', $typeid);
while($category1 = mysqli_fetch_assoc($getCategories)){
// DISPLAY DATA FROM $getdbdata
}
}
答案 0 :(得分:1)
你应该在MySQL中这样做。
SELECT * FROM `Data`
LEFT JOIN `Type` ON `Type`.id = `Data`.type
LEFT JOIN `Category` ON `Category`.id = `Data`.category
WHERE `Data`.section = '$selectValue'
我在猜表结构。此外,如果您需要在每个表上检查selectValue,那么您需要将它们添加到查询的WHERE部分。