0

I'm trying to create nested menu via mysql and php.

When I'm creating multidimensional array from this:

$ref = [];
$items = [];
foreach($row as $row) {
    $thisRef =& $ref[$row->id];
    $thisRef['parent_id'] = $row->parent_id;
    $thisRef['menu_id'] = $row->menu_id;
    $thisRef['type'] = $row->type;
    $thisRef['id'] = $row->id;
    if($row->parent_id == 0) {
        $items[] =& $thisRef;
    } else {
        $ref[$row->parent_id]['sub'][] =& $thisRef;
    }
}

it generates array like this.

Array(
    [0] => Array
        (
            [parent_id] => 0
            [menu_id] => 10
            [type] => p
            [id] => 93
        )
    [1] => Array
        (
            [sub] => Array
                (
                    [0] => Array
                        (
                            [parent_id] => 88
                            [menu_id] => 10
                            [type] => b
                            [id] => 92
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            parent_id] => 92
                                            [menu_id] => 8
                                            [type] => c
                                            [id] => 94
                                        )
                                )
                        )
                     [1] => Array
                        (
                            [parent_id] => 88
                            [menu_id] => 8
                            [type] => c
                            [id] => 90
                        )

                )

            [parent_id] => 0
            [menu_id] => 5
            [type] => p
            [id] => 88
        )
    )
)

and it's good but I'd like to change name of second array key sub I mean if array all ready has sub, second sub would be subofsub, I tried to check it with isset and array_key_exists but it just messes my array.

1
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Jul 26, 2017 at 20:00

1 Answer 1

0

Honestly not really following the question and a little confused with the by refs.

Are you simply trying to do something like this?

<?php

$ref   = [];
$items = [];
$subCount = 0;

foreach ($row as $row) {
    $thisRef = &$ref[$row->id];
    $thisRef['parent_id'] = $row->parent_id;
    $thisRef['menu_id'] = $row->menu_id;
    $thisRef['type'] = $row->type;
    $thisRef['id'] = $row->id;
    if($row->parent_id == 0) {
        $items[] = &$thisRef;
    } else {
        $subCount++;
        $ref[$row->parent_id]['sub-' . $subCount][] = &$thisRef;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

its similar to what i want but its not, i want to check somehow if array all ready has sub and put in that sub renamed sub something like this. array[1]->[sub]->[0]->[subofsub]
If we expressed that as an integer value we could call it 'depth' yes? Are you familiar with the concept of recursion? Not sure I want to try and explain using your existing code. Might be worth reading up some on the topic? Posts like: stackoverflow.com/questions/10782810/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.