In PHP is there a built-in function to replace this foreach loop?












0














In order to integrate icons into the navbar I use key=>value arrays to store a file name with an icon tag. In the navbar I use a foreach loop to build dropdown menus or index into an array for an individual navlink. This also allows to dynamically build and alter the dropdown menus very easily.



$homepage = array(
'index.php'=>'<i class="fa fa-home"></i>'
);
$guestpages = array(
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>'
);
$logout = array(
'logout.php'=>'<i class="fa fa-sign-out"></i>'
);
$pages = array($homepage,$guestpages,$logout);


I also parse the URL to determine what page the client is viewing.



$pagename = basename($_SERVER['PHP_SELF']);


And in order to associate the parsed URL with the appropriate icon tag from the $pages array, I currently use a nested foreach loop:



foreach ($pages as $pagearray) {
foreach ($pagearray as $page => $icon) {
if($pagename == $page) {
$pageicon = $icon;
}
}
}


And what I'd like to do instead is something like this:



$pageicon = $pages[?][$pagename];


Does a similar alternative solution exist?










share|improve this question






















  • Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
    – deceze
    Nov 13 '18 at 1:44


















0














In order to integrate icons into the navbar I use key=>value arrays to store a file name with an icon tag. In the navbar I use a foreach loop to build dropdown menus or index into an array for an individual navlink. This also allows to dynamically build and alter the dropdown menus very easily.



$homepage = array(
'index.php'=>'<i class="fa fa-home"></i>'
);
$guestpages = array(
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>'
);
$logout = array(
'logout.php'=>'<i class="fa fa-sign-out"></i>'
);
$pages = array($homepage,$guestpages,$logout);


I also parse the URL to determine what page the client is viewing.



$pagename = basename($_SERVER['PHP_SELF']);


And in order to associate the parsed URL with the appropriate icon tag from the $pages array, I currently use a nested foreach loop:



foreach ($pages as $pagearray) {
foreach ($pagearray as $page => $icon) {
if($pagename == $page) {
$pageicon = $icon;
}
}
}


And what I'd like to do instead is something like this:



$pageicon = $pages[?][$pagename];


Does a similar alternative solution exist?










share|improve this question






















  • Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
    – deceze
    Nov 13 '18 at 1:44
















0












0








0







In order to integrate icons into the navbar I use key=>value arrays to store a file name with an icon tag. In the navbar I use a foreach loop to build dropdown menus or index into an array for an individual navlink. This also allows to dynamically build and alter the dropdown menus very easily.



$homepage = array(
'index.php'=>'<i class="fa fa-home"></i>'
);
$guestpages = array(
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>'
);
$logout = array(
'logout.php'=>'<i class="fa fa-sign-out"></i>'
);
$pages = array($homepage,$guestpages,$logout);


I also parse the URL to determine what page the client is viewing.



$pagename = basename($_SERVER['PHP_SELF']);


And in order to associate the parsed URL with the appropriate icon tag from the $pages array, I currently use a nested foreach loop:



foreach ($pages as $pagearray) {
foreach ($pagearray as $page => $icon) {
if($pagename == $page) {
$pageicon = $icon;
}
}
}


And what I'd like to do instead is something like this:



$pageicon = $pages[?][$pagename];


Does a similar alternative solution exist?










share|improve this question













In order to integrate icons into the navbar I use key=>value arrays to store a file name with an icon tag. In the navbar I use a foreach loop to build dropdown menus or index into an array for an individual navlink. This also allows to dynamically build and alter the dropdown menus very easily.



$homepage = array(
'index.php'=>'<i class="fa fa-home"></i>'
);
$guestpages = array(
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>'
);
$logout = array(
'logout.php'=>'<i class="fa fa-sign-out"></i>'
);
$pages = array($homepage,$guestpages,$logout);


I also parse the URL to determine what page the client is viewing.



$pagename = basename($_SERVER['PHP_SELF']);


And in order to associate the parsed URL with the appropriate icon tag from the $pages array, I currently use a nested foreach loop:



foreach ($pages as $pagearray) {
foreach ($pagearray as $page => $icon) {
if($pagename == $page) {
$pageicon = $icon;
}
}
}


And what I'd like to do instead is something like this:



$pageicon = $pages[?][$pagename];


Does a similar alternative solution exist?







php arrays loops






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 1:38









Derek Roberts

187




187












  • Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
    – deceze
    Nov 13 '18 at 1:44




















  • Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
    – deceze
    Nov 13 '18 at 1:44


















Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
– deceze
Nov 13 '18 at 1:44






Why the three separate arrays? If you'd just have $pages = ['index.php' => ...], you could simply do $pages[basename($_SERVER['PHP_SELF'])]
– deceze
Nov 13 '18 at 1:44














1 Answer
1






active

oldest

votes


















3














Since your pagename must be unique, you can build your array in a single dimension, like this:



$pages = [
'index.php'=>'<i class="fa fa-home"></i>',
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>',
'logout.php'=>'<i class="fa fa-sign-out"></i>',
];


Then just use:



$icon = $pages[basename($_SERVER['PHP_SELF'])] ?? '<some default>';


[Edit] Alternatively, you can use array_merge() to combine your arrays:



$pages = array_merge($homepage, $guestpages, $logout);





share|improve this answer























  • This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
    – Derek Roberts
    Nov 13 '18 at 1:49












  • Then just use array_merge() to combine them.
    – Alex Howansky
    Nov 13 '18 at 1:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272535%2fin-php-is-there-a-built-in-function-to-replace-this-foreach-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Since your pagename must be unique, you can build your array in a single dimension, like this:



$pages = [
'index.php'=>'<i class="fa fa-home"></i>',
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>',
'logout.php'=>'<i class="fa fa-sign-out"></i>',
];


Then just use:



$icon = $pages[basename($_SERVER['PHP_SELF'])] ?? '<some default>';


[Edit] Alternatively, you can use array_merge() to combine your arrays:



$pages = array_merge($homepage, $guestpages, $logout);





share|improve this answer























  • This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
    – Derek Roberts
    Nov 13 '18 at 1:49












  • Then just use array_merge() to combine them.
    – Alex Howansky
    Nov 13 '18 at 1:50
















3














Since your pagename must be unique, you can build your array in a single dimension, like this:



$pages = [
'index.php'=>'<i class="fa fa-home"></i>',
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>',
'logout.php'=>'<i class="fa fa-sign-out"></i>',
];


Then just use:



$icon = $pages[basename($_SERVER['PHP_SELF'])] ?? '<some default>';


[Edit] Alternatively, you can use array_merge() to combine your arrays:



$pages = array_merge($homepage, $guestpages, $logout);





share|improve this answer























  • This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
    – Derek Roberts
    Nov 13 '18 at 1:49












  • Then just use array_merge() to combine them.
    – Alex Howansky
    Nov 13 '18 at 1:50














3












3








3






Since your pagename must be unique, you can build your array in a single dimension, like this:



$pages = [
'index.php'=>'<i class="fa fa-home"></i>',
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>',
'logout.php'=>'<i class="fa fa-sign-out"></i>',
];


Then just use:



$icon = $pages[basename($_SERVER['PHP_SELF'])] ?? '<some default>';


[Edit] Alternatively, you can use array_merge() to combine your arrays:



$pages = array_merge($homepage, $guestpages, $logout);





share|improve this answer














Since your pagename must be unique, you can build your array in a single dimension, like this:



$pages = [
'index.php'=>'<i class="fa fa-home"></i>',
'createaccount.php'=>'<i class="fa fa-university"></i>',
'login.php'=>'<i class="fa fa-sign-in"></i>',
'logout.php'=>'<i class="fa fa-sign-out"></i>',
];


Then just use:



$icon = $pages[basename($_SERVER['PHP_SELF'])] ?? '<some default>';


[Edit] Alternatively, you can use array_merge() to combine your arrays:



$pages = array_merge($homepage, $guestpages, $logout);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 1:52

























answered Nov 13 '18 at 1:44









Alex Howansky

35k55581




35k55581












  • This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
    – Derek Roberts
    Nov 13 '18 at 1:49












  • Then just use array_merge() to combine them.
    – Alex Howansky
    Nov 13 '18 at 1:50


















  • This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
    – Derek Roberts
    Nov 13 '18 at 1:49












  • Then just use array_merge() to combine them.
    – Alex Howansky
    Nov 13 '18 at 1:50
















This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
– Derek Roberts
Nov 13 '18 at 1:49






This would complicate the navbar construction though, because index.php and logout.php are individual links while createaccount.php and login.php are both in the same dropdown menu. So on the navbar I use a loop: foreach($guestpages as...) to build the dropdown menu containing the links to createaccount.php and login.php. I wouldn't be able to create individual dropdown menus from the $pages array, and don't want to maintain a seperate $pages array that is not nested.
– Derek Roberts
Nov 13 '18 at 1:49














Then just use array_merge() to combine them.
– Alex Howansky
Nov 13 '18 at 1:50




Then just use array_merge() to combine them.
– Alex Howansky
Nov 13 '18 at 1:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272535%2fin-php-is-there-a-built-in-function-to-replace-this-foreach-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python