这是我的URL:
http://example.com/shop-login.php?currency=USD&shipping=9.12&itemCount=2&item_name_1=+2014+New+Arriva+EGO+1500mAh+Big+Vape+HaKa+Twist+Variable+Voltage+Electronic+Cigarette+Battery+-+3+Optional+Colors&item_quantity_1=1&item_price_1=23.52&item_options_1=color%3A+Color%2C+pid%3A+5&item_name_2=+2014+hot+sell+2200mah+ego+Electronic+Cigarette+Battery+with+kgo+1+week+big+capacity+-+8+Optional+Colors&item_quantity_2=1&item_price_2=13&item_options_2=color%3A+Black%2C+pid%3A+6&Shipping_Option=EMS-Express
和PHP代码如下:
$item_count = (int) $_GET['itemCount'];
$i = 1;
$subtotal = (int) 0.00;
$grandtotal = (int) 0.00;
$items = array();
for ($i = 1; $i <= $item_count; $i++) {
$items[$i]['name'] = $_GET["item_name_".$i];
echo "item_name_".$i .'='. $items[$i]['name'] .'<br />' ;
$items[$i]['quantity'] = $_GET["item_quantity_".$i];
echo $items[$i]['quantity'] .'<br />' ;
$items[$i]['price'] = $_GET["item_price_".$i];
echo $items[$i]['price'] .'<br />' ;
$items[$i]['options'] = $_GET["item_options_".$i];
echo $items[$i]['options'] .'<br />' ;
//$item1 = explode (',', $items[$i]['options']);
//print_r($item1);
$subtotal += ($items[$i]['quantity'] * $items[$i]['price']);
$grandtotal = ($subtotal + $shipping_cost);
我让$items[$i]['options']
具有如下值:
color: Yellow, pid: 5
和
color:黑色,pid: 6
在for循环中用于2个产品。
我需要将color和pid作为for循环中的两个独立变量。值分隔符是,我尝试使用explosion .
我对如何获得$items[$i]['options']
内的变量作为循环内的单独变量感到困惑。对于某些产品,会有更多的变量,如风味、大小等。
如何获得颜色,pid或其他值,如风味,大小等作为每个产品id的单独变量。例:color_1, pid_1, size_1
etc. etc
更新:我是这样做的:
$itemexp1 = explode (",", $items[$i]['options']);
foreach ($itemexp1 as $key => $dat){
$pid[$key] = explode(":", $dat);
}
试试这个:
$items = $items[$i]['options'];
$firstArray = explode(",", $items[0]);
$secondArray = explode(":", $firstArray[0]);
$color = $secondArray[1];
$firstArray = explode(",", $items[0]);
$secondArray = explode(":", $firstArray[1]);
$pId = $secondArray[1];
echo $color . " " . $pId;