我有一个数组,每个元素都是这样的字符串:
$items[0] = "*1*x *2858* in *1* with selected image to print at *http://mysite.com/test.jpg*"
和
$items[1] = "*2*x *2353* in *2* with selected image to print at *http://mysite.com/test1.jpg*"
如何将此数组元素拆分为二维数组,使其变为:
$items[0][quantity] == 1
$items[0][product_id] == 2858
$items[0][variation} == 1
$items[0][selectedImageUrl] == "http://mysite.com/test.jpg"
和
$items[1][quantity] == 2
$items[1][product_id] == 2353
$items[1][variation} == 2
$items[1][selectedImageUrl] == "http://mysite.com/test1.jpg"
非常感谢你的帮助!
@卡尔
这就是我在应用您的代码时所拥有的。
我将它应用于我的情况,这就是我所拥有的:
function parse_my_str($str){
$bits = explode(' ', $str);
$out['selectedImageUrl'] = array_pop($bits);
$out['product_id'] = $bits[1];
$out['variation'] = $bits[3];
$bits = explode('*', $str);
$out['quantity'] = $bits[1];
return $out;
}
$items = explode("||", $_SESSION['mp_shipping_info']['special_instructions']);
foreach ($items as $i => $v) {
$items[$i] = parse_my_str($v);
print_r($items[$i]);
}
但我有
Array ( [selectedImageUrl] => *http://youparkrudely.com/files/2012/04/2011-Aston-Martin-Rapide-026.jpg* [product_id] => *2858* [variation] => *1* [quantity] => 1 ) Array ( [selectedImageUrl] => [product_id] => [variation] => [quantity] => )
@卡尔
Array (
[selectedImageUrl] => *http://youparkrudely.com/files/2012/04/2011-Aston-Martin-Rapide-026.jpg*
[product_id] => *2858*
[variation] => *1*
[quantity] => 1 )
Array ( [selectedImageUrl] => [product_id] => [variation] => [quantity] => )