Php 尝试使用foreach和switch语句显示数组中的值

Php 尝试使用foreach和switch语句显示数组中的值,php,arrays,foreach,switch-statement,Php,Arrays,Foreach,Switch Statement,因此,我正在尝试为wordpress中的一些自定义post类型构建所需的函数,但在我尝试弄清楚如何动态调用变量之前,我需要使此代码正常工作。我想我的问题可能在数组本身或Foreach循环中。当我运行代码时,我希望得到一个高、中、低价位的结果,但它只显示了高价位,三次。我做错了什么 <?php //variables that will be pulled from multiple post types and fields //p_high, med and low are produ

因此,我正在尝试为wordpress中的一些自定义post类型构建所需的函数,但在我尝试弄清楚如何动态调用变量之前,我需要使此代码正常工作。我想我的问题可能在数组本身或Foreach循环中。当我运行代码时,我希望得到一个高、中、低价位的结果,但它只显示了高价位,三次。我做错了什么

<?php

//variables that will be pulled from multiple post types and fields
//p_high, med and low are product multipliers

$client_lvl =array("High","Med", "Low");
$p_high= 2;
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type=  'Flat';

//Loops through array to display high, medium, and low price

Foreach ($client_lvl as $lvl)
{
If ($lvl ='High'){
$multiplier=$p_high;}
elseif($lvl ='Med')
{$multiplier=$p_med;
$m_high=$p_high;}
else {
$multiplier=$p_low;
$m_high=$p_high;}

//calculator that determines price based on multiplier type (m_type)

switch ($m_type) {

     case 'Area':
         if (isset($m_high)){
            $price_high= $m_high * $area; 
            echo "The High price was \${$price_high}<br/>";
            }

         $price= $multiplier * $area;
         echo "The {$lvl} price is \${$price}<br/>";

         break;

     case 'Image':
     if (isset($m_high)){
            $price_high= $m_high * $img_count *$cogs;
            echo "The High price was \${$price_high}<br/>";
            } 

         $price= $multiplier * $img_count *$cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Commission':
     if (isset($m_high)){
            $price_high= $m_high + $cogs;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier + $cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Flat':
     if (isset($m_high)){
            $price_high= $m_high;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Commission+Image':
     if (isset($m_high)){
            $price_high= $m_high + ($img_cost*$img_count);
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier + ($img_cost*$img_count);
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case'Price':
     if (isset($m_high)){
            $price_high= $m_high * $cogs;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier * $cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;
}}

/*End goal is to display the $price variable next to the product depending on if client has the High, medium, or low rate. Rate will be pulled from "client" data, m_type and multipliers will be pulled from "Product group" data, and all other variables will be found in the "product" data. 
If client is not logged in, multiplier uses high value.<br><br>



*/
?>
在“如果”条件下,应选中“不分配”。请尝试以下操作:

<?php

//variables that will be pulled from multiple post types and fields
//p_high, med and low are product multipliers

$client_lvl =array("High","Med", "Low");
$p_high= 2;
$p_med= 1.5;
$p_low =1.1;
$cogs= 10;
$img_count =3;
$img_cost= 10;
$area= 40;
$m_type=  'Flat';

//Loops through array to display high, medium, and low price

Foreach ($client_lvl as $lvl)
{
If ($lvl =='High'){
$multiplier=$p_high;}
elseif($lvl =='Med')
{$multiplier=$p_med;
$m_high=$p_high;}
else {
$multiplier=$p_low;
$m_high=$p_high;}

//calculator that determines price based on multiplier type (m_type)

switch ($m_type) {

     case 'Area':

         if (isset($m_high)){
            $price_high= $m_high * $area; 
            echo "The High price was \${$price_high}<br/>";
            }

         $price= $multiplier * $area;
         echo "The {$lvl} price is \${$price}<br/>";

         break;

     case 'Image':
     if (isset($m_high)){
            $price_high= $m_high * $img_count *$cogs;
            echo "The High price was \${$price_high}<br/>";
            } 

         $price= $multiplier * $img_count *$cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Commission':
     if (isset($m_high)){
            $price_high= $m_high + $cogs;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier + $cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Flat':
     if (isset($m_high)){
            $price_high= $m_high;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier;
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case 'Commission+Image':
     if (isset($m_high)){
            $price_high= $m_high + ($img_cost*$img_count);
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier + ($img_cost*$img_count);
         echo "The {$lvl} price is \${$price}<br/>";
         break;

     case'Price':
     if (isset($m_high)){
            $price_high= $m_high * $cogs;
            echo "The High price was \${$price_high}<br/>";
            } 
         $price= $multiplier * $cogs;
         echo "The {$lvl} price is \${$price}<br/>";
         break;
}}

/*End goal is to display the $price variable next to the product depending on if client has the High, medium, or low rate. Rate will be pulled from "client" data, m_type and multipliers will be pulled from "Product group" data, and all other variables will be found in the "product" data. 
If client is not logged in, multiplier uses high value.<br><br>



*/
?>

希望有帮助

在if语句中,为了进行比较,必须使用double equal==而不是简单的equal


简单的等号用于变量赋值,因此$level将始终等于high。

您在if语句中使用了一个等号。单个equal是赋值运算符。您需要在此处使用比较运算符==

因此,在代码中,当您想要将变量$lvl与字符串'high'进行比较时,实际上是将值'high'赋值给$lvl。这就是为什么你总是得到高价值

您的代码应该如下所示:

foreach ($client_lvl as $lvl) {
    If ($lvl == 'High'){
        $multiplier=$p_high;
    } elseif($lvl =='Med'){
        $multiplier=$p_med;
        $m_high=$p_high;
    } else {
        $multiplier=$p_low;
        $m_high=$p_high;
    }

或者您可以使用switch语句来代替。

谢谢,看来应该可以解决这个问题了。今晚我要玩它。