$requests = array("add : milk",
"remove: milk",
"add : pickles");
$request_option = "add : remove : checkout : quantity_upd";
$request_option_split = explode(":",$request_option);
// print_r($request_option_split);
function shoppingCart($requests,$option){
$j = sizeof($requests);
$basket = array();
if ($option[1] == 'remove') {
echo "hahah";
}else{
echo "f***";
}
for ($i=0 ; $i < $j ; $i++) {
$temp = explode(":",$requests[$i]);
// print_r($temp);
switch ($option[1]) {
case $option[0]://add
$basket[$temp[1]] = 1;
break;
case $option[1]://remove
echo "asdf";
unset($basket[0]);
break;
case $option[3]://quantity_upd
$basket[$temp[1]] = $basket[$temp[1]] + intval($temp[2]);
break;
case $option[2]://checkout.
$j = sizeof($basket);
for ($i=0; $i < $j; $i++) {
unset($basket[$i]);
}
break;
default:
echo "hello world";
break;
}
}
print_r($basket);
}
shoppingCart($requests, $request_option_split);
I am trying to split each element from an array, then if the element matches the option, it will execute. For example, I split out the first element of $request
array, and $temp[0]
will be "add"
. However, when I put the $temp[0]
into the switch statement, it doesn't equal to the string "add"
. That is so werid.
I use gettype
function to check it, every element I got from the $request
array is string. but they are not equal to "string"
. I am so confusing. I tried the the $str = (string)$temp[0]
as well. it still doesn't work. can somebody explain it to me please?