We help IT Professionals succeed at work.

php array

impressionexpress
on
Below is my php and my html, they are menu items. the most important part is checking the perm in each array and compare it with a $_SESSION variable
Array
(
    [0] => Array
        (
            [url] => index.php
            [class] => fas fa-list
            [name] => My Application's
            [perm] => 10
        )

    [1] => Array
        (
            [url] => application.php
            [class] => fas fa-file-medical
            [name] => New Application
            [perm] => 10
        )

)

Open in new window

if($_SESSION['permission'] > PERM){
     <a href="URL"><i class="CLASS"></i> NAME </a>
}

Open in new window

Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
You'd need to loop through your array:

foreach ($yourArray as $menu) {
    if ($_SESSION['permission'] > $menu['perm']) {
        printf('<a href="%s"><i class="%s"></i> %s </a>', $menu['url'], $menu['class'], $menu['name']);
    }
}

Open in new window

Author

Commented:
What if my array looked like this:
Array
(
    [0] => Array
        (
            [url] => index.php
            [class] => fas fa-list
            [name] => My Application's
            [perm] => 10
        )

    [1] => Array
        (
            [class] => fas fa-list
            [name] => New Application
            [perm] => 10
            [0] => Array
                (
                    [url] => consumer_application.php
                    [class] => fas fa-list
                    [name] => Consumer Application
                    [perm] => 10
                )

            [1] => Array
                (
                    [url] => commercial_application.php
                    [class] => fas fa-list
                    [name] => Commercial Application
                    [perm] => 10
                )

        )

)

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Then you'd need to loop within the loop, or use one of the recursive functions.

Having said that, your data structure looks a little off and that'll make it a bit trickier. You inner array doesn't have any keys to it - thery're just indexed elements on the parent array so you can't just loop. Are you sure that's exactly how your array is built? If you control the building of the array, then you should change it and wrap the inner arry into a property/key of the parent array

Author

Commented:
I can control the building of the array

The purpose of the inner array is for a submenu. do you have a better way to do build the array ?
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
OK. If a menu item has child menu items, then it makes it easier to deal with if you wrap those in a key of the parent, so you end up with an array that could look something like this:

$menuItems = [
    [
        "url" => 'index.php',
        "class" => 'fas fa-list',
        "name" => "My Application's",
        "perm" => 10,
    ],
    [
        "class" => "fas fa-file-medical",
        "name" => "New Application",
        "perm" => 15,
        "children" => [
            [
                "url" => "consumer_application.php",
                "class" => "fas fa-list",
                "name" => "Consumer Application",
                "perm" => 10,
            ],
            [
                "url" => "commercial_application.php",
                "class" => "fas fa-list",
                "name" => "Commercial Application",
                "perm" => 10,
            ],
        ],
    ],
];

Open in new window

Now when you look through the outer array, you can check each element for 'children' key and loop through that.

Author

Commented:
Copy & paste of your array gives me the following output. is it normal I get "[0] => Array" on line 24 ?
Array
(
    [0] => Array
        (
            [url] => index.php
            [class] => fas fa-list
            [name] => My Application's
            [perm] => 10
        )

    [1] => Array
        (
            [class] => fas fa-list
            [name] => New Application
            [perm] => 10
            [subMenuItems] => Array
                (
                    [url] => consumer_application.php
                    [class] => fas fa-list
                    [name] => Consumer Application
                    [perm] => 10
                )

            [0] => Array
                (
                    [url] => commercial_application.php
                    [class] => fas fa-list
                    [name] => Commercial Application
                    [perm] => 10
                )

        )

)

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Nope - you'll have an error in your code somewhere - probably a missing brakcet or something. Double-check it or post up your code.

The output you should end up with is this:

Array
(
    [0] => Array
        (
            [url] => index.php
            [class] => fas fa-list
            [name] => My Application's
            [perm] => 10
        )

    [1] => Array
        (
            [class] => fas fa-file-medical
            [name] => New Application
            [perm] => 15
            [children] => Array
                (
                    [0] => Array
                        (
                            [url] => consumer_application.php
                            [class] => fas fa-list
                            [name] => Consumer Application
                            [perm] => 10
                        )

                    [1] => Array
                        (
                            [url] => commercial_application.php
                            [class] => fas fa-list
                            [name] => Commercial Application
                            [perm] => 10
                        )

                )

        )

)

Open in new window

Author

Commented:
How would I unset() the array where [url] => index.php
Array
(
    [0] => Array
        (
            [url] => index.php
            [class] => fas fa-list
            [name] => My Application's
            [perm] => 10
        )

    [1] => Array
        (
            [url] => application.php
            [class] => fas fa-file-medical
            [name] => New Application
            [perm] => 10
        )

)

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Hi,

You can run the array_filter() on the array to exclude the items you don't want:

$filtered = array_filter($menuItems, function($item) {
    return !isset($item['url']) || $item['url'] != "index.php";
});

Open in new window

Having said that, if you have control over building the array in the first place, why not just exclude it when you build it?

Author

Commented:
A long story short, I can't exclude it when building it. but yes, I control over the array.

by looking at your code. im wondering if you understood

the result of what I need the unset to do is:
Array
(
    [1] => Array
        (
            [url] => application.php
            [class] => fas fa-file-medical
            [name] => New Application
            [perm] => 10
        )

)

Open in new window


then I would need to sort the array so that they the first array is 0 and not 1
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Hi,

That's exactly what my code does - have you tried running it?

Once you've done that, you just need to pass the array through array_values() to reset the keys:

// filter out the index.php elements
$myArray = array_filter($myArray, function($item) {
    return !isset($item['url']) || $item['url'] != "index.php";
});

// reset the index
$myArray = array_values($myArray);

var_dump($myArray);

Open in new window