2011-01-15 7 views
-1

Ich habe ein Array so.Zuordnung von Buchstaben anstelle von Zahlen in einer 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." 

);

Meine Kunden entschieden sich, Buchstaben anstelle von Zahlen in der Datenbank zu verwenden. Wenn ich eine foreach verwende, um auszuführen, was passieren muss, verwenden sie Zahlen und nicht die entsprechenden Buchstaben.

Hier ist meine 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;'>":""; 
    } 
    } 

Die $ MSDS_LIST ist ein Array, das so aussieht.

Array ([0] => R)

+0

Was ist die Frage? Sie können assoziative Arrays in PHP mit alphanumerischen Schlüsseln wie in Ihrem ursprünglichen Beispiel verwenden. Wenn Sie darüber foreach wie in 'foreach ($ ARR_MSDS als $ k => $ v) {', sollte $ k der Schlüssel sein, und $ v sollte der Wert sein. Versuchen Sie 'print_r' auf Ihrem Array und stellen Sie sicher, dass es die Schlüssel hat, die Sie erwarten. – MightyE

+0

Ja, es gibt keine Frage hier:/ – sevenseacat

+0

Ich sehe eine '10' in den Schlüsseln, die heimlich in die Liste gerutscht wurde ... ist das das Problem? –

Antwort

1

Ich glaube, man die Zahlen für die von Ihrem Master-Array in den $imgPArry Variable ist verwirrend. Die Zahlen, die Sie auf der $imgPArry Referenz sehen, sind aufgrund der explode geschieht auf einen einzelnen Wert von Ihrem Master-Array. Mit anderen Worten funktioniert die foreach ordnungsgemäß mit den Buchstaben.

Während jeder Iteration der foreach wird eine explode mit dem Wert passiert, brechen Sie es bei der :. Dieses neue Subarray hat dann mehrere Teile, abhängig davon, wie viele : in dem Wert waren. Diese sind durch Nummern referenziert.

Zum Beispiel

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

... wird ...

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

, dass Ihre Frage nicht beantworten?

Verwandte Themen