Skip to content
Advertisement

How to reverse this json

I have a problem about array reversing. I tried array_reverse(…,true) But it is not working. May be the place that i applied is wrong. So i already attached my php code and json result below. Plz help me to reverse this json.

$conn = mysqli_connect(HOST,USER,PASS,DB);
$sql = mysqli_query($conn,"SELECT story_id,story_name,story_author,story_cover_link FROM short_story 
 WHERE parent=0");

// parent categories node
$categories = array("Categories" => array());

while ($row = mysqli_fetch_array($sql,MYSQLI_ASSOC))
    {
     $story_id = $row['story_id'];

     $ssql = mysqli_query($conn,"SELECT story_id,chapter_name,chapter_link FROM short_story WHERE  parent='$story_id'");

     // single category node
     $category = array(); // temp array
     $category["story_id"] = $row["story_id"];
     $category["story_name"] = $row["story_name"];
     $category["story_author"] = $row["story_author"];
     $category["story_cover_link"] = $row["story_cover_link"];
     $category["chapters"] = array(); // subcategories again an array

     while ($srow = mysqli_fetch_array($ssql,MYSQLI_ASSOC))
         {
         $subcat = array(); // temp array
         $subcat["story_id"] = $srow['story_id'];
         $subcat["chapter_name"] = $srow['chapter_name'];
         $subcat["chapter_link"] = $srow['chapter_link'];
         // pushing sub category into subcategories node

         array_push($category["chapters"], $subcat);
     }

     // pushing sinlge category into parent
     array_push($categories["Categories"], $category);
 }

 echo json_encode($categories);

This is resultant json. What i want is, i want to reverse this. After reversion

{
"Categories": [{
    "story_id": "1",
    "story_name": "Nethuu",
    "story_author": "Gimhani Thennakon",
    "story_cover_link": "http://appzoneproduction.xyz/sinhala_novels/Cover/sonduru_nawodaya.jpg",
    "chapters": [{
        "story_id": "7",
        "chapter_name": "Nethu - 1",
        "chapter_link": "http://appzoneproduction.xyz/sinhala_novels/Novel/gal%20kanda%20walawwa.pdf"
    }, {
        "story_id": "8",
        "chapter_name": "Nethu - 2",
        "chapter_link": "http://appzoneproduction.xyz/sinhala_novels/Novel/gal%20kanda%20walawwa.pdf"
    }, {
        "story_id": "9",
        "chapter_name": "Nethu - 3",
        "chapter_link": "http://appzoneproduction.xyz/sinhala_novels/Novel/gal%20kanda%20walawwa.pdf"
    }]
}, {
    "story_id": "2",
    "story_name": "Sewwandi",
    "story_author": "Gayana S Lakmali",
    "story_cover_link": "http://appzoneproduction.xyz/sinhala_novels/Cover/gal%20kanda%20walawwa.jpg",
    "chapters": [{
        "story_id": "10",
        "chapter_name": "Sewwandi - 1",
        "chapter_link": "http://appzoneproduction.xyz/sinhala_novels/Novel/gal%20kanda%20walawwa.pdf"
    }, {
        "story_id": "11",
        "chapter_name": "Sewwandi - 2",
        "chapter_link": "http://appzoneproduction.xyz/sinhala_novels/Novel/gal%20kanda%20walawwa.pdf"
    }]
}]
 }

Thank you

Advertisement

Answer

If what you are looking for is to just have ID’s go descending instead of ascending, you dont need to revise the whole array. You could just query the db to deliver data in that order and just run through it as you already do.

Change this:

$sql = mysqli_query($conn,"SELECT story_id,story_name,story_author,story_cover_link FROM short_story 
 WHERE parent=0");

To this:

$sql = mysqli_query($conn,"SELECT story_id,story_name,story_author,story_cover_link FROM short_story 
 WHERE parent=0 ORDER BY story_id DESC");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement