Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:17:14+00:00 2026-06-01T23:17:14+00:00

I have an array with each element is a string like this: $items[0] =

  • 0

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] => )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:17:16+00:00Added an answer on June 1, 2026 at 11:17 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have grid like this. How to change index for each element, i.e. to
Suppose I have an array that mimics a database table. Each array element represents
I have a two dimensional JSON array where each element contains several attributes. The
I have an array of structs and I intend to pass each element of
I have a multidimensional array of bools, with each element set to true: var
I have to load an array of doubles from a file, multiply each element
I will have a string like this: Bob is a boy. Bob is 1000
I have an array that contains 10 elements, each element contains . How do
I have a string like this: $string = 'Joe Bob John Doe Amber Page
I have a string MyString like this: var MyString = 32,43,545,34,23,; I'd like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.