--- id: php-arrays-update title: "PHP Arrays Update" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["PHP λ°°μ—΄ μˆ˜μ •"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "arrays", "update", "unset"] raw_sources: ["https://www.w3schools.com/php/php_arrays_update.asp"] applied_in: [] github_commit: "" --- # [[PHP Arrays Update]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Forgetting `unset($x)` after a by-reference foreach loop is a genuine bug trap β€” the source demonstrates it directly: after the loop, `$x` STILL points at the array's last element, so a later `$x = "ice cream";` silently overwrites that last array element too, even though the loop has already finished. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Update by index/key** β€” same syntax as access: `$cars[1] = "Ford";` or `$cars["year"] = 2024;`. [S1] - **By-reference foreach for bulk update** β€” `foreach ($cars as &$x) { $x = "Ford"; }` mutates every element of the original array. [S1] - **`unset($x)` after the loop is mandatory** β€” without it, `$x` remains a reference to the array's last element, and any later reassignment of `$x` silently corrupts that array element. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic index update: `$cars = array("Volvo", "BMW", "Toyota"); $cars[1] = "Ford";`. [S1] - Basic key update: `$cars = array("brand" => "Ford", ...); $cars["year"] = 2024;`. [S1] - Bulk by-reference update: `foreach ($cars as &$x) { $x = "Ford"; } unset($x);` β€” all items become "Ford". [S1] - Bug demonstration (unset omitted): `foreach ($cars as &$x) { $x = "Ford"; } $x = "ice cream"; var_dump($cars); // last element of $cars is now "ice cream"!`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **unset() μƒλž΅μ˜ μœ„ν—˜**: μ°Έμ‘° 반볡 ν›„ unset()을 ν˜ΈμΆœν•˜μ§€ μ•ŠμœΌλ©΄ $xκ°€ λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό 계속 μ°Έμ‘°ν•˜λ©°, 이후 $xλ₯Ό μž¬ν• λ‹Ήν•˜λ©΄ 배열이 μ˜λ„μΉ˜ μ•Šκ²Œ λ³€κ²½λœλ‹€λŠ” 점이 버그 예제둜 직접 증λͺ…됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” foreach μ°Έμ‘° 반볡 ν›„ unset()을 λΉ λœ¨λ¦¬λŠ” 것이 μ‹€μ „μ—μ„œ 자주 λ°œμƒν•˜λŠ” 버그 νŒ¨ν„΄μ΄λ―€λ‘œ λ°˜λ“œμ‹œ κΈ°μ–΅ν•΄μ•Ό ν•œλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Forgetting unset() after a by-reference loop corrupts the array (PHP): ```php $cars = array("Volvo", "BMW", "Toyota"); foreach ($cars as &$x) { $x = "Ford"; } $x = "ice cream"; // BUG: $x still references $cars' last element! var_dump($cars); // last element is now "ice cream", not "Ford" ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Arrays Access]], [[PHP Looping Foreach]], [[PHP Arrays Add]] - **μ°Έμ‘° λ§₯락:** λ°°μ—΄ ν•­λͺ© μˆ˜μ • β€” ν•­λͺ© μΆ”κ°€(Add) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Update Array Items β€” https://www.w3schools.com/php/php_arrays_update.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Update Array Items" page (Astra wiki-curation, P-Reinforce v3.1 format).