在foreach中分配字母而不是数字

时间:2011-01-15 16:56:45

标签: php arrays foreach

我有一个像这样的数组。

$ARR_MSDS=array(
'K' => "Latex Warning: Caution this product contains natural rubber latex, which may cause allergic reactions!",
'L' => "Additional Shipping Charges: Please note that standard shipping costs do not apply.  This item requires an oversized and/or overweight shipping surcharge. Total shipping charges will be calculated upon receipt of order and you will be called with exact total.",
'M' => "Bariatric Rated: Please note that this product has a Bariatric rating." ,
'B' => "HazMat: Non-returnable.  This item is classified as a hazardous material by DOT and has the following shipping restrictions: ground shipping only (no air shipments) and only ship to the 48 continuous states.  Additional shipping charges may apply.",
'P' => "Refrigerated Item: Shipped on ice only Monday - Wednesday.",
'E' => "Prescription Drug or Device: This item may only be sold to a licensed healthcare facility.",
'A' => "Special Order: This item is a special order and is non-returnable.  Please ensure this is the item you want.  If you have any questions, please contact us.  It may be drop shipped from the manufacturer.",
'X' => "Earth-Friendly: This item is certified Earth-Friendly.",
'V' => "Controlled Drug: Requires a DEA license and may only be shipped to the address on the license.",
10  => "Class II Drug:  Non-refundable.  This drug requires an original DEA Form 222 to be in our hands prior to shipping your order.  Please contact us if you require assistance.",
'T' => "No Return: Cannot be sent back.",
'C' => "Short-Dated Item: This item has a shorter shelf life, usually less than 6 months, and is priced accordingly.  This item is non-returnable."

);

我的客户决定在数据库中使用字母而不是数字。当我使用foreach来执行需要发生的事情时,他们使用数字而不是相应的字母。

这是我的预言。

foreach($ARR_MSDS as $k=>$v){
    $imgPArry  = explode(":",$v);
    $imgPath   = $imgPArry[0];
    $imgTile =  "<span ><strong>".$imgPArry[0]."</strong>";
    $imgTile1 = $imgPArry[1];
    if($imgPArry[2]!='')
    {
        $tileMain = $imgTile ." :".$imgTile1." ".$imgPArry[2]."</span>";
    }else{
        $tileMain = $imgTile ." :".$imgTile1."</span>";
    }
    if(is_array($MSDS_LIST)){
        //onmouseover=\"Tip('<strong>Please call customer service at 1(800) 748-5665 to order item</strong>', BALLOON, true, ABOVE, true, OFFSETX, -17)\"
        $MSDS_LIST_RESULT.=(in_array($k,$MSDS_LIST))?"<img src='/images/msdx/$imgPath.gif' onmouseout=\"hideDiv()\" onmouseover=\"showDiv('$tileMain')\"   style='padding:2px;margin:0px;'>":"";
    }
    else{
        $MSDS_LIST_RESULT=($k==$MSDS_LIST)?"<img src='/images/msdx/$imgPath.gif'   title='$v' style='padding:2px;'>":"";
    }
    }

$ MSDS_LIST是一个看起来像这样的数组。

数组([0] =&gt; R)

1 个答案:

答案 0 :(得分:1)

我相信你会混淆$imgPArry变量中的数字与主数组的数字混淆。您在$imgPArry上看到的引用数字是由于主数组中的单个值发生的explode。换句话说,foreach与字母一起正常工作。

foreach的每次迭代过程中,值explode发生,并在:处将其分解。这个新的子阵列有几个部分,具体取决于值中:的数量。这些都是用数字引用的。

例如,

"Latex Warning: Caution this product contains natural rubber latex, which may cause allergic reactions!"

... ...变为

$imgPArry[0] = "Latex Warning";
$imgPArry[1] = " Caution this product contains natural rubber latex, which may cause allergic reactions!";

这会回答你的问题吗?