I am developing a PHP 7 application that has a list of users and their dietary requirements.
If any users have dietary requirements, I need to show a link to a page that can display them; if none users have such requirements, then this is not displayed.
My array $userslooks like this:
[
[ 'name' => 'Andy', 'diet' => '' ],
[ 'name' => 'Bob', 'diet' => 'Vegeterian' ],
[ 'name' => 'John', 'diet' => '' ]
]
So, in the example above, Bob has dietary requirements, and a button should be shown.
My plan to determine whether to show or not the button includes a loop through the entire array $users, and if it finds any elements of the array 'diet'that are not empty, it shows the button, for example.
$show_dietary_button = false;
foreach ($users as $user) {
if ($user['diet'] !== '') {
$show_dietary_button = true;
break;
}
}
if ($show_dietary_button) {
echo '<a href="#">Show Dietary Requirements</a>';
}
Is there an easier way to do this, i.e. a way to say that any of the elements of the array with the key "diet" had data in them?
source
share