3

I have a string like following...

Array ([product_name] => this is a product [product_desc] => some descripyion [cat_id] => 3)

This is looking like as an array but this is a string. If I use echo then it prints the same result.

$someVariable = "Array ([product_name] => this is a product [product_desc] => some descripyion [cat_id] => 3)";

echo $someVariable;

Result:

Array ([product_name] => this is a product [product_desc] => some descripyion [cat_id] => 3)

I need it to convert to an array so that I can do the following..

echo $someVariable['product_name'];

and get the following result

this is a product

Is there any way to do this ?

Thanks

16
  • 1
    This is very very hard, if not impossible. Why do you have "arrays" in this format to begin with? You should use a better serialisation format than var_dump. Commented May 26, 2014 at 10:33
  • Agree with @deceze, please explain why you have an array in string format? Commented May 26, 2014 at 10:34
  • using explode function you can do this Commented May 26, 2014 at 10:34
  • what is the need of putting this printed format of array in string. what you are doing actully? Commented May 26, 2014 at 10:35
  • 1
    What about if you run serialize() on the array before you submit the form and then convert it back into an array with unserialize(). Commented May 26, 2014 at 11:17

3 Answers 3

5

serialize the data:

<input type="hidden" name="data" valaue='<?php print_r(serialize($yourData));?>'>

And then unserialize:

<?php 
    $youralldata = unserialize($_POST['data']);
    print_r($youralldata);
?>
Sign up to request clarification or add additional context in comments.

Comments

2
$someVariable = "Array ([product_name] => this is a product [product_desc] => some descripyion [cat_id] => 3)";

preg_match_all('/\[(.*?)\]/', $someVariable, $keys);
preg_match_all('/=> (.*?) ?[\[|\)]/', $someVariable, $values);

$someVariable = array_combine($keys[1], $values[1]);

This converts the string back into an array.

2 Comments

Good point. You would have to validate your input. It's definitely better to avoid having to do this.
Yes, avoiding this whole situation is the only reasonable answer. var_dump is not suitable to serialise arbitrary values.
0
 function stringToArray($string){
    $pattern = '/\[(.*?)\]|=>\s+[\w\s\d]+/';
    preg_match_all($pattern,$string,$matches);
    $result = array();
    foreach($matches[0] as $i => $match){
        if($i%2 == 0){
                    $key = trim(str_ireplace(array("[","]"),"",$match));
            $value = trim(str_ireplace(array("=>"),"",$matches[0][$i+1]));
            $result[$key] = $value;
        }
    }
    return $result;
}

$someVariable =   "Array ([product_name] => this is a product [product_desc] => some descripyion [cat_id] => 3)";

$someVariable = stringToArray($someVariable);

echo $someVariable['product_name'];

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.