0

I keep product options as string in DB, and I need to parse it (string to array).

String is:

{option_name:option_id{variant_id:variant_name}}

How can I parse it with using PHP?

1
  • It looks like a JSON string. Why not just use json_decode()? Commented Nov 4, 2010 at 20:06

2 Answers 2

7

Why not use serialize() and unserialize()?

Sign up to request clarification or add additional context in comments.

1 Comment

Instead of converting your product to the string you have above, take the product (I'm assuming it's an array) and then serialize it serialize($myproductarray). That turns it into a portable string. When you need the info from the database, get the string, and unserialize($myproductstring) to get it back into an array.
1

You didn't detail the field formats so here's a guestimate that you can tweak:

preg_match('/^{(\w+):(\d+){(\d+):(\w+)}}$/', '{an_option_name:123{456:a_variant_name}}', $matches);

$option_name  = $matches[1];
$option_id    = $matches[2];
$variant_id   = $matches[3];
$variant_name = $matches[4];

Comments

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.