How can I check inside many array and change value of certain variables

I have this session called "order" that displays similar to the image attached. (Arrays for each meal with days).
However I want to add an IF statement that if the users email is "admin" then make all monday & fridays equal to 0. while keeping wednesday the same.
Is there a way to do this?
qty.pnghere is what i wrote so far but i got stuck on how to reach monday & friday and replace their numbers with 0
$emails = "admin1,admin2,admin3";
$finalArr = explode(',', $emails );

if(isset($_SESSION['email']))
{
	$email_check = $_SESSION['email'];
	if (in_array($email_check, $finalArr))
	{

foreach($_SESSION['order'] as $day=>$amount)
{
     foreach($amount as $indivdual => $newDay)
    {

    }
}
}
	
}

Open in new window

LVL 1
Jazzy 1012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
$isAdmin = true; /* change to check if is admin */
$order  = isset($_SESSION['order']) ? $_SESSION['order'] : false;
if ($order && $isAdmin) {
  foreach($order as $o) {
    foreach($o['days'] as $day) {
       $day['monday'] = 0;
       $day['friday'] = 0;
  }
}

Open in new window

0
Julian HansenCommented:
Revised to use admin
$emails = "admin1,admin2,admin3";
$finalArr = explode(',', $emails );

$email = isset($_SESSION['email']) ? $_SESSION['email'] : false;

$isAdmin = ($email && in_array($email, $finalArr));
$order  = isset($_SESSION['order']) ? $_SESSION['order'] : false;

if ($order && $isAdmin) {
  foreach($order as $o) {
    foreach($o['days'] as $day) {
      $day['monday'] = 0;
      $day['friday'] = 0;
  }
}

Open in new window

0
Jazzy 1012Author Commented:
I got the error "illegal string offset 'friday' or 'monday'
0
Jazzy 1012Author Commented:
it also did not work probably because the error stopped it?

I also printed out the '$day' in the last foreach and its the qtys after the days not the days, does this help?
0
Julian HansenCommented:
Corrected code
<?php
session_start();

$_SESSION['order'] = [[
	"meal" => 85,
	"protein" => 10800,
	"days" => [
		"monday" => 3,
		"wednesday" => 3,
		"friday" => 0
	]
],[
	"meal" => 1,
	"protein" => 1001,
	"days" => [
		"monday" => 2,
		"wednesday" => 4,
		"friday" => 0
	]
]];

$_SESSION['email'] = 'admin1';

$emails = "admin1,admin2,admin3";
$finalArr = explode(',', $emails );

$email = isset($_SESSION['email']) ? $_SESSION['email'] : false;

$isAdmin = ($email && in_array($email, $finalArr));
$order  = isset($_SESSION['order']) ? $_SESSION['order'] : false;

if ($order && $isAdmin) {
  foreach($order as &$o) {
    $o['days']['monday'] = 0;
    $o['days']['friday'] = 0;
  }
}

echo "<pre>" . print_r($order, true) . "</pre>";

Open in new window


NOTE: I have hardecoded the Session for demonstration purposes. Please modify the code before using it by changing to match your specific environment.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.