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

Jenz
  • 8,280
  • 7
  • 44
  • 77
Tanvir
  • 1,635
  • 2
  • 17
  • 31
  • 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`. – deceze May 26 '14 at 10:33
  • Agree with @deceze, please explain why you have an array in string format? – Zander Rootman May 26 '14 at 10:34
  • using explode function you can do this – user1187 May 26 '14 at 10:34
  • what is the need of putting this printed format of array in string. what you are doing actully? – Satish Sharma May 26 '14 at 10:35
  • I am passing the values of a form to the several steps of pages.here is the example to make it more clear..I have a form..when I click submit button it shows the preview using the values of the form submitted as post method and then I set the values to a hidden form element to the preview page and again after clicking the submit button I take the values to the next page..But when I set the values of the first form to a form element it converts to the string..and when I click submit button I get the values but at the string format.. – Tanvir May 26 '14 at 10:35
  • please first explain how you got an array in string format? – user1187 May 26 '14 at 10:35
  • The format of string is quite strange, but if you really need to convert it to real array you should remove "Array ([", then use explode by "[" and then each element explode by "] =>" – santik May 26 '14 at 10:37
  • If there is no easy solution then I have do this in a different way.. – Tanvir May 26 '14 at 10:44
  • you can use the hidden values right – user1187 May 26 '14 at 10:49
  • after submitting from preview page where should it go? – user1187 May 26 '14 at 10:50
  • after submitting from preview page I will insert the data to the database – Tanvir May 26 '14 at 10:53
  • then you can use the hidden values – user1187 May 26 '14 at 10:55
  • you have to submit the for use the hidden id for inserting – user1187 May 26 '14 at 10:55
  • somebody already answered something like this, cant find it though – user1978142 May 26 '14 at 10:59
  • Maybe a duplicate of this : http://stackoverflow.com/questions/7025909/create-array-printed-with-print-r – Sly May 26 '14 at 11:07
  • 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(). – Teaqu May 26 '14 at 11:17

3 Answers3

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);
?>
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Bipul Khan
  • 697
  • 1
  • 5
  • 10
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.

Teaqu
  • 2,953
  • 1
  • 14
  • 21
  • Good point. You would have to validate your input. It's definitely better to avoid having to do this. – Teaqu May 26 '14 at 11:25
  • Yes, avoiding this whole situation is the only reasonable answer. `var_dump` is not suitable to serialise arbitrary values. – deceze May 26 '14 at 11:27
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'];
noobie-php
  • 6,817
  • 15
  • 54
  • 101
Dastagir
  • 982
  • 8
  • 14