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

Jazzy 1012
Jazzy 1012 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
$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

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

Commented:
I got the error "illegal string offset 'friday' or 'monday'

Author

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?
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial