|  | 
	                
  
    | 
	 
        พี่ๆครับ พอมีตัวอย่าง foreach แบบ 2 array มั้ยครับ แนะนำหน่อยครับผมจะเอาไปดูเป็นแนวทาง     |  
    |  |  
 
              
  
    | 
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เพิ่มเติม 
 
  ตอบความคิดเห็นที่ : 1 เขียนโดย : 009 เมื่อวันที่ 2022-10-14 11:16:37 
 รายละเอียดของการตอบ ::
 หรือ จะใช้ array_walk() หรือ พวก array_xxx() ต่างๆ มาร่วมด้วยก็ได้
 แต่ทว่า ฟังก์ชัน array_xxx() ส่วนใหญ่จะมี loop รันอยู่เบื้องหลัง
 ทั้งหมดสามารถใช้ for, foreach, while, do...while แทนได้
 ดังนั้น ถ้าจำไม่ได้ อย่าจำ programming ไม่เน้นจำ ศึกษาให้เข้าใจพื้นฐาน loop ก็เพียงพอแล้ว
 หรือถ้าอยากใช้ค่อยดู syntax ก็ไม่เสียหลาย ...(แบ่งสมองไปจำอย่างอื่นบ้าง
  ) 
 อีกอย่างที่สำคัญ คือ ฝึกดู syntax แล้วนำไปประยุกต์ใช้ให้เป็น
 อันนี้ส่วนใหญ่ที่บอกว่ายากคือพื้นฐานไม่แน่น
 อารมณ์ประมาณตอนเรียนไม่ค่อยเห็นภาพ(ไม่เก็ต) จนน่าเบื่อ
 เลยกระโดดข้ามไปดูเรื่องที่ตัวเอง(คิดว่า) สนใจ ซับซ้อนเลย (แนววิชามาร)...
 ...สุดท้ายหนีไม่พ้นธาตุไฟเข้าแทรก
  
 
 
 
 ตัวอย่าง foreach(กรณี 2 arrays ถูกจัดเรียงไม่เหมือนกัน)
 
 <!DOCTYPE html>
<html>
<head>
<style>
table {
  border-collapse: collapse;
  width: 50%;
  margin-left: auto;
  margin-right: auto;
}
table, th, td {
  color: #456;
  padding: 8px;
  text-align: center;
  border: 0.5px solid #DDD;
}
table>caption {
  color: #456;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}
th {color: #EFF; background-color: #899;}
tr:hover {background-color: #D6EEEE;}
</style>
</head>
<body>
<?php
// =============== Data in Array 2D (database results) ==============
$sal = [
	["id" => "1", "name" => "Chelsea Mooney", "salary" => "160216"],
	["id" => "2", "name" => "Griffin Farrell", "salary" => "119315"],
	["id" => "3", "name" => "Oren Floyd", "salary" => "33868"],
	["id" => "4", "name" => "Chelsea Stanton", "salary" => "112107"],
	["id" => "5", "name" => "Lynn Hardin", "salary" => "181863"],
	["id" => "6", "name" => "Ariel Lindsay", "salary" => "157499"],
	["id" => "7", "name" => "Jescie Mullen", "salary" => "130681"],
	["id" => "8", "name" => "Lyle Hughes", "salary" => "99090"],
	["id" => "9", "name" => "Carl Keller", "salary" => "33497"],
	["id" => "10", "name" => "Trevor Wilkerson", "salary" => "51135"]
];
$exps = [
	["id" => "1", "expenses" => "7366"],
	["id" => "2", "expenses" => "5183"],
	["id" => "3", "expenses" => "6511"],
	["id" => "4", "expenses" => "8811"],
	["id" => "5", "expenses" => "7949"],
	["id" => "6", "expenses" => "8993"],
	["id" => "7", "expenses" => "7190"],
	["id" => "8", "expenses" => "6913"],
	["id" => "9", "expenses" => "5489"],
	["id" => "10", "expenses" => "5236"]
];
// ==========================================
/* ถ้าจะดูตัวอย่างใด ให้ comment ตัวอย่างอื่นก่อน */
// ตัวอย่าง 1
foreach ($sal as $s) {
    foreach ($exps as $e) {
        if ($s["id"] == $e["id"]) {
            $ni[$s["name"]] = (int)$s["salary"] - (int)$e["expenses"];
            break;
        }
    }	
}
// ตัวอย่าง 2
/*
foreach ($sal as $s) {
    for ($i=0; $i<count($exps); $i++) {
        if ($s["id"] == $exps[$i]["id"]) {
            $ni[$s["name"]] = (int)$s["salary"] - (int)$exps[$i]["expenses"];
            break;
        }
    }	
}
*/
// ตัวอย่าง 3
/*
$id = array_column($exps, "id");
array_multisort($id, $exps);
$i = 0;
foreach ($sal as $s) {
    $ni[$s["name"]] = (int)$s["salary"] - (int)$exps[$i++]["expenses"];
}
*/
//print_r($ni);
/*
Array
(
    [Chelsea Mooney] => 152850
    [Griffin Farrell] => 114132
    [Oren Floyd] => 27357
    [Chelsea Stanton] => 103296
    [Lynn Hardin] => 173914
    [Ariel Lindsay] => 148506
    [Jescie Mullen] => 123491
    [Lyle Hughes] => 92177
    [Carl Keller] => 28008
    [Trevor Wilkerson] => 45899
)
*/
?>
<!-- ตัวอย่างการ loop แสดงในตาราง HTML -->
<table>
	<caption>Monthly savings</caption>
	<tr>
    		<th>Name</th>
        		<th>Net Income</th>
    	</tr>
<?php
foreach ($ni as $k=>$v) {
?>
	<tr>
    		<td><?php echo $k; ?></td>
        		<td><?php echo number_format($v); ?></td>
    	</tr>
<?php
}
?>  
</table>
</body>
</html>
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2022-10-15 12:16:40 | By :
                            009 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองสลับ array element จะเห็นภาพ เช่น 
 Code
 
 $exps = [
	["id" => "1", "expenses" => "7366"],
	["id" => "5", "expenses" => "7949"],
	["id" => "6", "expenses" => "8993"],
	["id" => "2", "expenses" => "5183"],
	["id" => "3", "expenses" => "6511"],
	["id" => "4", "expenses" => "8811"],
	["id" => "7", "expenses" => "7190"],
	["id" => "8", "expenses" => "6913"],
	["id" => "9", "expenses" => "5489"],
	["id" => "10", "expenses" => "5236"]
];
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2022-10-15 12:25:30 | By :
                            009 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 |  |