$tbl_industries = array(
array(
'id' => 1,
'industry' => 'Building',
'category' => 'Air Conditioning'
),
array(
'id' => 2,
'industry' => 'Building',
'category' => 'Roofing'
),
array(
'id' => 3,
'industry' => 'Security',
'category' => 'Cameras'
)
);
foreach($tbl_industries as $industry) {
$industry_name = $industry['industry'];
$industries[$industry_name][] = $industry['category'];
}
var_dump($industries);
OR
與數據庫交互的代碼:
$con = mysqli_connect('localhost', 'root', '', 'db_name');
$query = "SELECT * FROM tbl_industries";
if($result = mysqli_query($con, $query)) {
while($industry = mysqli_fetch_assoc($result)) {
$industry_name = $industry['industry'];
$industries[$industry_name][] = $industry['category'];
}
mysqli_free_result($result);
var_dump($industries);
}
感謝這是最好的迴應,非常感謝您的瞭解。 –