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

PHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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

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

Chris Stanyon

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
impressionexpress

ASKER
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 ?
Chris Stanyon

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

ASKER
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Stanyon

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

impressionexpress

ASKER
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

Chris Stanyon

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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
impressionexpress

ASKER
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
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.