--- id: php-json title: "PHP JSON" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["json_encode", "json_decode", "PHP JSON"] 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", "json"] raw_sources: ["https://www.w3schools.com/php/php_json.asp"] applied_in: [] github_commit: "" --- # [[PHP JSON]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `json_decode()` returns a PHP OBJECT by default β€” accessed via `->property` syntax β€” but passing `true` as the second argument switches it to an ASSOCIATIVE ARRAY instead, accessed via `["key"]` syntax, meaning the exact same JSON string can be decoded into two structurally different PHP representations depending on one boolean flag. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`json_encode($value)`** β€” converts a PHP value (array/object) to a JSON string. [S1] - **`json_decode($jsonString)`** β€” converts JSON into a PHP object by default. [S1] - **`json_decode($jsonString, true)`** β€” the second `true` argument decodes into an associative array instead. [S1] - **Associative array β†’ JSON object** β€” `array("Peter"=>35, ...)` encodes to a JSON object `{"Peter":35,...}`. [S1] - **Indexed array β†’ JSON array** β€” `array("Volvo", "BMW", "Toyota")` encodes to a JSON array `["Volvo","BMW","Toyota"]`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Encode associative array (β†’ JSON object): `$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43); echo json_encode($age);`. [S1] - Encode indexed array (β†’ JSON array): `$cars = array("Volvo", "BMW", "Toyota"); echo json_encode($cars);`. [S1] - Decode to object (default): `$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); echo $obj->Peter;`. [S1] - Decode to associative array (with `true`): `$arr = json_decode($jsonobj, true); echo $arr["Peter"];`. [S1] - Loop through decoded object: `foreach($obj as $key => $value) { echo $key . " => " . $value . "
"; }`. [S1] - Loop through decoded associative array: same `foreach ($arr as $key => $value)` pattern works identically. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λ””μ½”λ”© κ²°κ³Ό νƒ€μž…μ˜ 이쀑성**: json_decode()λŠ” 기본적으둜 객체λ₯Ό λ°˜ν™˜ν•˜μ§€λ§Œ, 두 번째 인자둜 trueλ₯Ό μ£Όλ©΄ μ—°κ΄€ λ°°μ—΄λ‘œ λ°˜ν™˜λœλ‹€λŠ” 점이 핡심 차이둜 강쑰됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” λ‚˜μ΄ 데이터λ₯Ό JSON으둜 인코딩/λ””μ½”λ”©ν•˜κ³  foreach둜 μˆœνšŒν•˜λŠ” 것이 API 데이터 κ΅ν™˜μ˜ ν‘œμ€€ νŒ¨ν„΄μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Decoding JSON to an associative array vs. object (PHP): ```php $jsonobj = '{"Peter":35,"Ben":37,"Joe":43}'; $obj = json_decode($jsonobj); // object: $obj->Peter $arr = json_decode($jsonobj, true); // associative array: $arr["Peter"] ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Callback Functions]], [[PHP Arrays Associative]], [[PHP Exceptions]] - **μ°Έμ‘° λ§₯락:** JSON 인코딩/λ””μ½”λ”© β€” μ˜ˆμ™Έ 처리(Exceptions & Errors) μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP and JSON β€” https://www.w3schools.com/php/php_json.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP and JSON" page (Astra wiki-curation, P-Reinforce v3.1 format).