Cách nhóm Array theo biến từ Array có sẵn (array cũ) bằng hàm Foreach trong PHP. Đây là một cách thức giúp chúng ta sắp xếp các kết quả được load ra từ CSDL hoạc một hàm nào đó theo một biến nhất định có trong array đó.
Tùm lại là muốn chia, phân loại, group một array lộn xộn thành các array có điểm chung để chúng ta tiện bố trí và sử dụng theo mục đích của chúng ta.
Sắp xếp lại Array theo biến trong PHP
Tôi có một Array là Options như sau (Array này là mảng khi chúng ta truy vấn MySQl và được trả về chẳng hạn)
$options = array(
array("brand" => "Puma","code" => "p01","name" => "Puma One","id" => "1"),
array("brand" => "Puma","code" => "p02","name" => "Puma Two","id" => "2"),
array("brand" => "Puma","code" => "p03","name" => "Puma Three","id"=>"3"),
array("brand" => "Nike","code" => "n01","name" => "Nike One","id"=>"4"),
array("brand" => "Nike","code" => "n02","name" => "Nike Two","id"=>"5"),
array("brand" => "Nike","code" => "n03","name" => "Nike Three","id"=>"6"),
array("brand" => "Nike","code" => "n04","name" => "Nike Four","id"=>"7"),
array("brand" => "Adidas","code" => "a01","name" => "Adidas One","id"=>"8"),
array("brand" => "Adidas","code" => "a02","name" => "Adidas Two","id"=>"9"),
array("brand" => "Adidas","code" => "a03","name" => "Adidas Three","id"=>"10"),
array("brand" => "Adidas","code" => "a04","name" => "Adidas Four","id"=>"11"),
array("brand" => "Adidas","code" => "a05","name" => "Adidas Five","id"=>"12"),
array("brand" => "Adidas","code" => "a06","name" => "Adidas Six","id"=>"13")
);
Bây giờ tôi muốn phân loại theo thương hiệu. Mỗi thương hiệu tạo một Array để hiển thị trong các thẻ khác nhau. Kết quả như sau:
<optgroup label="Puma"> <option value="1">Puma One [p01]</option> <option value="2">Puma Two [p02]</option> <option value="3">Puma Three [p03]</option> </optgroup> <optgroup label="Nike"> <option value="4">Nike One [n01]</option> <option value="5">Nike Two [n02]</option> <option value="6">Nike Three [n03]</option> <option value="7">Nike Four [n04]</option> </optgroup> <optgroup label="Adidas"> <option value="8">Adidas One [a01]</option> <option value="9">Adidas Two [a02]</option> <option value="10">Adidas Three [a03]</option> <option value="11">Adidas Four [a04]</option> <option value="12">Adidas Five [a05]</option> <option value="13">Adidas Six [a06]</option> </optgroup>
Thực hiện nào, ta sẽ dùng hàm foreach để sắp xếp và phân loại Array options trên và hiển thị nó ra theo nhóm kết quả đã được sắp xếp
$result = array(); foreach ($options as $data) { $brand = $data['brand']; if (isset($result[$brand])) { $result[$brand][] = $data; } else { $result[$brand] = array($data); } } // Cho hiển thị kết quả theo nhóm Brand foreach ($result as $brand => $lists) { echo "<optgroup label=\"$brand\">\n"; foreach ($lists as $list) { $name=$list['name']; $code=$list['code']; $id=$list['id']; echo "<option value=\"$id\">$name [$code]</option>\n"; } echo "</optgroup>\n"; }
Rất đơn giản phải không? Mọi thắc mắc vui lòng comment bên dưới nhé!
Nguyễn Duy Hiếu / ITcuaBan.com