1

我有一个数组,每个元素都是这样的字符串:

$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] => )
4

3 回答 3

1

更新了新字符串,preg_match()改为使用:

$str = "*1*x *2858* in *1* with selected image to print at *http://mysite.com/test.jpg*";
$arr = parse_my_str($str);
print_r($arr);

function parse_my_str($str){
  preg_match_all('!\\*(.*?)\\*!', $str, $m);
  return array(
    'quantity' => $m[1][0],
    'product_id' => $m[1][1],
    'variation' => $m[1][2],
    'selectedImageUrl' => $m[1][3],
  );
}

对于您的示例,您将使用如下函数:

foreach ($items as $k => $v) $items[$k] = parse_my_str($v);
于 2012-04-12T21:36:03.843 回答
1

您可以使用preg_match()解决方案和正则表达式。不确定我是否完全匹配,但这是一个简单的例子。

// Regular Expression
$pattern = '/^\*(\d+)\*x (\d+) in (\d+) with selected image to print at (.+)$/';
preg_match( $pattern, $string, $matches );

如果你var_dump() $matches得到这个:

array(5) {
[0]=> string(74) "*2*x 2353 in 2 with selected image to print at http://mysite.com/test1.jpg" 
[1]=> string(1) "2" 
[2]=> string(4) "2353" 
[3]=> string(1) "2" 
[4]=> string(27) "http://mysite.com/test1.jpg" 
}

您将不得不遍历您的项目数组。

foreach ( $array as $key => $value ) :

    preg_match( $pattern, $value, $matches );

    $items[$key]['quantity'] = $matches[1];
    $items[$key]['product_id'] = $matches[2];
    $items[$key]['variation'] = $matches[3];
    $items[$key]['selectedImageURL'] = $matches[4];

endforeach;

我还建议使用自定义功能。

如果你需要一些关于正则表达式的帮助,我建议从这里开始:http ://www.regular-expressions.info/

于 2012-04-12T21:54:08.217 回答
0
<?php
$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";

foreach ($items as $key=>$item){
    $temp = explode(" ", $item);
    $items[$key] = array();
    $items[$key]['quantity'] = $temp[0];
    $items[$key]['product_id'] = $temp[1];
    $items[$key]['variation'] = $temp[3];
    $items[$key]['selectedImageURL'] = $temp[10];
}

print_r($items);

沿着这些思路的东西应该起作用。当然,假设您的数量和变化保持不变。

于 2012-04-12T21:36:38.273 回答