我根据运输方法获得不同的文本字符串。例如一个订单可以返回下列任何一项:基于文本的php阵列和数组映射
productmatrix_Pick-up_at_Store
productmatrix_Standard
productmatrix_3-Day_Delivery
productmatrix_Pick-up_at_Store_-_Rush_Processing
productmatrix_2-Day_Delivery
productmatrix_1-Day_Delivery_-_Rush_Processing
我也有一个“地图”的生产代码,我需要配合到这些。关键是要匹配的文字。价值是我需要的。地图看起来是这样的:
$shipToProductionMap = array(
"1-day" => 'D',
"2-day" => 'E',
"3-day" => 'C',
"standard" => 'Normal',
"pick-up" => 'P'
);
我的目标是创建一个将返回从$shipToProductionMap
基于我传递给它的字符串正确的值的函数。喜欢的东西:
function getCorrectShipCode($text){
if(strtolower($text) == if we find a hit in the map){
return $valueFromMap;
}
}
因此,举例来说,如果我这样做,我会得到P
作为返回值:
$result = getCorrectShipCode('productmatrix_Pick-up_at_Store');
//$result = 'P';
如何是最好的办法做到这一点?
这似乎是一个非常hacky的方式来解决这个问题。您是否无法控制订单信息如何发送进行处理?为什么要传递字符串而不是对象或数组,这些对象或数组通过一些字符串比较解决方法精确地告诉您需要知道的内容? –