Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

if statement malfunction

I have this session variable:
$_SESSION['protiens']['wed']

Open in new window

it stores inside wed, the qty chosen along with the price.
when I print it out I get:
Array ( [3800] => Array ( [qty] => 1 [price] => 10 ) [2702] => Array ( [qty] => 1 [price] => 20 ) [4000] => Array ( [qty] => 0 [price] => 10 ) )

Open in new window


I have an if statement which is :
if($_SESSION['protiens']['wed'][$id]['qty'] == 0){echo " none";}

Open in new window

"none"  hides my div. but as you can see there are 2 ids where wed has qty = 1, but the if statement just goes on the last id, where the qty = 0, and hides my div. Is there a way where I make my if statement check if all the qty = 0, to hide, and if there is at least one qty that equals more than 0 to skip the if statement?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Please show full source.

Here is your above code in a working script - nothing wrong with the script so I am guessing it is somewhere in the code you have not shown us.
<?php
session_start();

$_SESSION['proteins'] = array();
$_SESSION['proteins']['wed'] = array ( 
	3800 => Array ( 
		'qty' => 1, 
		'price' => 10 
	), 
	2702 => Array  ( 
		'qty' => 1, 
		'price' => 20 
	),
	4000 => Array ( 
		'qty' => 0,
		'price' => 10 
	) 
);
$ids = array_keys($_SESSION['proteins']['wed']);
foreach($ids as $id) {
	if($_SESSION['proteins']['wed'][$id]['qty'] == 0){echo " none<br/>";}
	else {
		echo "Not None<br/>";
	}
}

Open in new window

Avatar of Jazzy 1012
Jazzy 1012

ASKER

The problem is that the $id, in this case is the last id which is "4000" which has the qty as zero. That is why the if statement gets executed. I want it to check every id and if there is at least 1 id that has a qty of greater than 0, to not execute the if statement.  But if I do a foreach loop then it will execute more than once. I just need a way to allow the if statement to check all the ids to see if the qty is = to 0 or not
@Julian: See also:
https://www.experts-exchange.com/questions/29017689/Make-an-array-show-the-subkey-and-put-it-in-a-query.html

Basically you need an iterator to walk through the nested arrays, find the quantities, and make if() decisions about what is found by the iteration process.
Try:
session_start();
...

# $qty will contain the ids/keys to all the array that contain qty>0
# so if it ends up being empty, then all subarrays have qty==0
$qty=Array();

# saving reference to $qty variable since the third argument to array_walk is not pass by reference. So, $result is just a "stub"
$result[]=&$qty;

# apply array_walk.  Using an anonymous function as the callback (http://php.net/manual/en/functions.anonymous.php)
array_walk($_SESSION['protiens']['wed'], function(&$arr,$key,&$dup){if( $arr['qty']>0 ){$dup[0][]=$key;}}, $result);

# delete  the "stub"
unset($result)

# instead of this
#    if($_SESSION['protiens']['wed'][$id]['qty'] == 0){echo " none";}
# you need:
if( empty($qty) )
{
 echo 'none';
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial