Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

ความแตกต่าง array_merge และ array + array (Union) ใน PHP

ทั้งคู่รวม Array แต่พฤติกรรมต่างกันกับ Key ที่ซ้ำ (Duplicate Keys)

array_merge() - รวมและ Re-index Numeric Keys

  • String Key ซ้ำ: Value ตัวหลัง Overwrite ตัวก่อน
  • Numeric Key: Re-index ใหม่ 0,1,2... (ไม่ Overwrite)
$arr1 = ['a' => 1, 'b' => 2, 0 => 'first'];
$arr2 = ['b' => 20, 'c' => 3, 0 => 'second'];

$result = array_merge($arr1, $arr2);
print_r($result);
/*
Array
(
    [a] => 1
    [b] => 20        <-- Overwrite: arr2 wins
    [0] => first     <-- Re-indexed to 0
    [c] => 3
    [1] => second    <-- Re-indexed to 1
)
*/

Union Operator (+) - รวมโดย Key ตัวแรกชนะ

  • String Key ซ้ำ: Value ตัวแรกคงอยู่ (First Wins)
  • Numeric Key: Key ตัวแรกคงอยู่ (ไม่ Re-index)
$arr1 = ['a' => 1, 'b' => 2, 0 => 'first'];
$arr2 = ['b' => 20, 'c' => 3, 0 => 'second'];

$result = $arr1 + $arr2;
print_r($result);
/*
Array
(
    [a] => 1
    [b] => 2         <-- Keep arr1 (First Wins)
    [0] => first     <-- Keep arr1 Key 0
    [c] => 3         <-- New key from arr2
    // arr2[0] ('second') ถูกทิ้ง เพราะ Key 0 มีอยู่แล้ว
)
*/

ตัวอย่างจากคำถามเดิม (ปรับปรุง)

$arr1 = ["p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110"];
$arr2 = ["t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125"];

// ไม่มี Key ซ้ำ -> ผลลัพธ์เหมือนกัน
var_dump($arr1 + $arr2);
var_dump(array_merge($arr1, $arr2));

// แต่ถ้ามี Key ซ้ำ:
$arr1 = ["p"=>"150", "q"=>"100"];
$arr2 = ["q"=>"999", "r"=>"200"]; // q ซ้ำ

echo "Union (+):\n";
var_dump($arr1 + $arr2); 
// q => 100 (arr1 wins)

echo "array_merge:\n";
var_dump(array_merge($arr1, $arr2));
// q => 999 (arr2 wins)

ตัวอย่าง Numeric Keys

$a = [0 => 'a', 1 => 'b'];
$b = [0 => 'x', 1 => 'y', 2 => 'z'];

echo "Union (+):\n";
var_dump($a + $b);
// [0 => 'a', 1 => 'b', 2 => 'z']  // Key 0,1 จาก $a, Key 2 จาก $b

echo "array_merge:\n";
var_dump(array_merge($a, $b));
// [0 => 'a', 1 => 'b', 2 => 'x', 3 => 'y', 4 => 'z']  // Re-index ทั้งหมด

array_merge_recursive() - รวมแบบ Recursive (สำหรับ Multi-dimensional)

$arr1 = ['user' => ['name' => 'John', 'tags' => ['php']]];
$arr2 = ['user' => ['email' => 'john@example.com', 'tags' => ['mysql']]];

// array_merge: user key ซ้ำ -> arr2 overwrites arr1 ทั้งหมด
print_r(array_merge($arr1, $arr2));
// ['user' => ['email' => ..., 'tags' => ['mysql']]]  // name หาย!

// array_merge_recursive: รวม Deep
print_r(array_merge_recursive($arr1, $arr2));
/*
['user' => [
    'name' => 'John',
    'tags' => ['php', 'mysql'],  // รวม Array
    'email' => 'john@example.com'
]]
*/

PHP 7.4+ Spread Operator (...)

$merged = [...$arr1, ...$arr2];
// เทียบเท่า array_merge() สำหรับ Numeric Keys
// แต่ String Keys: Later Overwrites Earlier (เหมือน array_merge)

$union = $arr1 + $arr2;
// Union Operator ยังคงเป็น First Wins

สรุป: ใช้ตัวไหนเมื่อไหร่?

สถานการณ์แนะนำ
Config/Defaults + User Override (User Wins)array_merge($defaults, $userConfig)
Defaults + User Config (Defaults Wins สำหรับ Key ที่มี)$userConfig + $defaults
รวม Indexed Arrays (ต้องการ Re-index)array_merge หรือ [...$a, ...$b]
รวม Indexed Arrays (เก็บ Key เดิม)$a + $b
Multi-dimensional Config Mergearray_merge_recursive หรือ Library (symfony/config)