1

I have an array with each element is a string like this:

$items[0] = "*1*x *2858* in *1* with selected image to print at *http://mysite.com/test.jpg*"

and

$items[1] = "*2*x *2353* in *2* with selected image to print at *http://mysite.com/test1.jpg*"

How can I split this array elements into a 2nd dimension array so that it becomes:

$items[0][quantity] == 1
$items[0][product_id] == 2858
$items[0][variation} == 1
$items[0][selectedImageUrl] == "http://mysite.com/test.jpg"

and

$items[1][quantity] == 2
$items[1][product_id] == 2353
$items[1][variation} == 2
$items[1][selectedImageUrl] == "http://mysite.com/test1.jpg"

Thanks so much for your help!


@Cal

This is what I have when applying your code.

I applied it to my situation and this is what I have:

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]);
}

But i've got

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

@Cal

    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] => )
0

3 Answers 3

1

Updated for new string, using preg_match() instead:

$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],
  );
}

For your example, you'd use the function like this:

foreach ($items as $k => $v) $items[$k] = parse_my_str($v);
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks so much @Cal. I posted my result above, could you please take a look?
have you see it now? it's below my original question. we are almost there, thanks so much!
what you've got is correct! delete print_r($items[$i]) and put print_r($items) at the end
I've got print_r($items[$i]) just to see the outcome. the problem is it has the star (*) in front and at the end of each second dimension array elements.
like this Array ( [0] => Array ( [selectedImageUrl] => youparkrudely.com/files/2012/04/… [product_id] => 2858 [variation] => 1 [quantity] => 1 ) [1] => ) Array ( [0] => Array ( [selectedImageUrl] => youparkrudely.com/files/2012/04/… [product_id] => 2858 [variation] => 1 [quantity] => 1 ) [1] => Array ( [selectedImageUrl] => [product_id] => [variation] => [quantity] => ) )
|
1

You could go with a preg_match() solution and a regular expression. Not sure if I matched things exactly but here is a simple example.

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

if you var_dump() $matches you'll get this:

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" 
}

You would have to loop through your array of items.

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;

I would also suggest a custom function.

If you need some help on regular expression I suggest starting here: http://www.regular-expressions.info/

Comments

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);

Something along those lines should work. Assuming, of course, that your quantity and variation remain the same.

5 Comments

explode() takes 2 arguments and this code does not do what the question is asking.
yes, I realize - I clicked post before I was done, my mistake
Almost there - here's a version of your code that will almost work, but the quantity is still wrong: codepad.org/8ERL5mSW
Yeah, I actually got around to testing...oh well, thanks for your help :)
i know. that's some real cool code! but thank you for trying. i do appreciate that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.