Link to home
Create AccountLog in
Avatar of impressionexpress
impressionexpress

asked on

php array

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

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of impressionexpress
impressionexpress

ASKER

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

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
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 ?
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.
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

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

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

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?
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
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account