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
)
)
if($_SESSION['permission'] > PERM){
<a href="URL"><i class="CLASS"></i> NAME </a>
}
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
)
)
)
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
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
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 ?
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,
],
],
],
];
Now when you look through the outer array, you can check each element for 'children' key and loop through that.
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
)
)
)
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:
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
)
)
)
)
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
)
)
Hi,
You can run the array_filter() on the array to exclude the items you don't want:
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";
});
Having said that, if you have control over building the array in the first place, why not just exclude it when you build it?
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:
then I would need to sort the array so that they the first array is 0 and not 1
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
)
)
then I would need to sort the array so that they the first array is 0 and not 1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window