Q:

Dynamic multidimensional array overrides it self

EDIT:
I've been experimenting for a while now and it seems that it could be the SQL that only give me one row per call, could I optimize it or write it in another way?

Trying to create/get a dynamic multidimensional array/menu from DB, but it wont print out all my rows in DB. Example: When printing out two rows I only get one with the SQL I got now, when using nested arrays display_children I also only get one row for each array. I try to explain this phenomen with the JSON examples below.
I've tried to send a variable through the function so each call adds it own "level" value to each variable inside the function, but did not manage that to work. 
This is an example of my result(made it more readable):
[
    {
        "category_id": "1",
        "title": "First test",
        "categories": [
            {
                "category_id": "2",
                "title": "Second category",
                "categories": [
                    {
                        "category_id": "3",
                        "title": "3",
                        "categories": ""
                    }
                ]
            }
        ]
    }
]

This is what I want/expect:
[
    {
        "category_id": "1",
        "title": "First test",
        "categories": [
            {
                "category_id": "2",
                "title": "Second category",
                "categories": [
                    {
                        "category_id": "3",
                        "title": "3",
                        "categories": ""
                    }
                ]
            },
            {
                "category_id": "4",
                "title": "Fourth category",
                "categories": ""
            }
        ]
    }
]

DB tables:

Here is the code:
<?php
/* DB info and so on above this line */
function display_children($parent) {

    if(!empty($parent)) {

        global $dbh;
        $query = "SELECT category.*,
                        GROUP_CONCAT(category_hierarchy.category_id SEPARATOR ',') AS subcategories
                    FROM        category 
                    LEFT JOIN   category_hierarchy
                        ON      category_hierarchy.category_parent_id = category.category_id
                    WHERE       category.type = 2 AND ";

        $queryArr   = array();
        $queryArrValue = array();

        $parents = explode(',', $parent);

        foreach($parents as $value) {
            $queryArr[]     = "(category.category_id = ?)";
            $queryArrValue[]    = $value;
        }

        $queryArr = implode(' OR ', $queryArr);

        $query .=   "(".$queryArr.")";
        $query .=   " ORDER BY category.sort_order ASC";

    // Prepare.
        $stmt = $dbh->prepare($query);

    // Execute.
        $stmt->execute($queryArrValue);

    // Fetch results.
        $categories = $stmt->fetchAll();

        $returnArr = array();

                    foreach($categories as $category) {

                        if(!empty($category['subcategories'])) {
                            $parent_arr = array(display_children($category['subcategories']));
                        } else {
                            $parent_arr = '';
                        }

                        $returnArr[] = array(
                            'category_id'   => $category['category_id'],
                            'title'         => $category['title'],
                            'slug'          => $category['slug'],
                            'url'           => $category['url'],
                            'type'          => $category['type'],
                            'sort_order'    => $category['sort_order'],
                            'categories'    => $parent_arr
                            );
                    }

                    return $returnArr;
    }

}

$arr = array();

// Query.

    $query =    "SELECT         category.*,
                                GROUP_CONCAT(category_hierarchy.category_id SEPARATOR ',') AS subcategories     
                FROM            category 
                LEFT JOIN       category_hierarchy
                    ON          category_hierarchy.category_parent_id = category.category_id
                WHERE           category.type = 1
                ORDER BY        category.sort_order ASC
                ";

    // Prepare.
        $stmt = $dbh->prepare($query);

    // Execute.
        $stmt->execute();

    // Fetch results.
        $categories = $stmt->fetchAll();

        $countedRows = count($categories);

        foreach($categories as $category) {
            $parent_arr = '';
            if(!empty($category['subcategories'])) {
                $parent_arr = array(display_children($category['subcategories']));
            } 

            $arr[] = array(
                    'category_id'   => $category['category_id'],
                    'title'         => $category['title'],
                    'slug'          => $category['slug'],
                    'url'           => $category['url'],
                    'type'          => $category['type'],
                    'sort_order'    => $category['sort_order'],
                    'categories'    => $parent_arr
            );
        }

/* Output JSON */
    header('Content-type: application/json');
    echo json_encode($arr);
    die();
?>

A:

It was the SQL, as I'm using GROUP_CONCAT I had to add GROUP BY
SELECT      
category.*,
GROUP_CONCAT(category_hierarchy.category_id SEPARATOR ',') AS subcategories 

FROM            category
LEFT JOIN       category_hierarchy    ON        category.category_id = category_hierarchy.category_parent_id
WHERE           category.type = '1'

GROUP BY        category.category_id
ORDER BY        category.sort_order ASC

See description to answer here

