Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: JSON tree do płaskiej struktury
Forum PHP.pl > Forum > PHP
psw779
Witam,

chciałbym z drzewa zapisanego rekurencyjnie w formacie JSON stworzyć strukturę płaską.

Próbkę pliku JSON załączam, a oczekiwany format miałby wyglądać tak (fragment):

id;parent_id
36;35
74;35
35;1
1:0
55;54
57;54
54;1
76:0
77:0

Oczywiście json_decode i ładowanie z pliku potrafię wykonać. Tylko co dalej...

JSON:
Kod
[
    {
        "id":1,
        "children":[
            {
                "id":35,
                "children":[
                    {
                        "id":36,
                        "children":[
                            
                        ]
                    },
                    {
                        "id":74,
                        "children":[
                            
                        ]
                    }
                ]
            },
            {
                "id":54,
                "children":[
                    {
                        "id":55,
                        "children":[
                            
                        ]
                    },
                    {
                        "id":57,
                        "children":[
                            
                        ]
                    }
                ]
            }
        ]
    },
    {
        "id":76,
        "children":[
            
        ]
    },
    {
        "id":77,
        "children":[
            
        ]
    }
]


Temat udało się rozwiązać następującym kodem:

  1.  
  2. $jsonArray = json_decode($jsonString, true);
  3.  
  4. function parseJsonArray($jsonArray, $parentID = 0)
  5. {
  6. $return = array();
  7. foreach ($jsonArray as $subArray) {
  8. $returnSubSubArray = array();
  9. if (isset($subArray['children'])) {
  10. $returnSubSubArray = parseJsonArray($subArray['children'], $subArray['id']);
  11. }
  12. $return[] = array('id' => $subArray['id'], 'parentID' => $parentID);
  13. $return = array_merge($return, $returnSubSubArray);
  14. }
  15.  
  16. return $return;
  17. }
  18.  
  19. print_r(parseJsonArray($jsonArray));
  20.  
nospor
No to musisz rekurencyjnie przeleciec po swojej tablicy.
  1. $ar = json_decode('[
  2. {
  3. "id":1,
  4. "children":[
  5. {
  6. "id":35,
  7. "children":[
  8. {
  9. "id":36,
  10. "children":[
  11.  
  12. ]
  13. },
  14. {
  15. "id":74,
  16. "children":[
  17.  
  18. ]
  19. }
  20. ]
  21. },
  22. {
  23. "id":54,
  24. "children":[
  25. {
  26. "id":55,
  27. "children":[
  28.  
  29. ]
  30. },
  31. {
  32. "id":57,
  33. "children":[
  34.  
  35. ]
  36. }
  37. ]
  38. }
  39. ]
  40. },
  41. {
  42. "id":76,
  43. "children":[
  44.  
  45. ]
  46. },
  47. {
  48. "id":77,
  49. "children":[
  50.  
  51. ]
  52. }
  53. ]');
  54.  
  55. //print_r($ar);
  56. ser($ar, 0);
  57.  
  58. function ser($children, $idR){
  59.  
  60. foreach ($children as $child){
  61. if (!empty($child->children))
  62. ser($child->children, $child->id);
  63. echo $child->id.';'.$idR.'<br />';
  64. }
  65. }
  66.  

Ot i cala filozofia smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.