Convert array string into an array

Hi - how do I convert an array string into an array (i.e. the following values into an array). It may already look like an array but PHP is seeing it as a string (just need to convert it please).


Array
(
    [plaintext] => This is an example CV.
    [filename] => 1345550846.doc
)

  1. Find the contents of the array(s). (Hint: preg_match_all)
  2. Foreach of those contents, explode on line breaks.
  3. Foreach line, explode on =>.
  4. Trim the first component. Left trim the second, right trim the last.
  5. the key is defined as the first element of the components, from index 1 to -1 (hint: substr). The value is defined as the remaining elements imploded on => (in case there is a => in the value).

Is there no easier way to convert it straight to an array?

What exactly is the output you want to achieve, as you already have an array…

Can you pass the variable to var_dump() so we can see how PHP is identifying the data

array(2) {
  ["plaintext"]=>
  string(23) "This is an example CV.
"
  ["filename"]=>
  string(14) "1345555572.doc"
}

Okay, is that the var_dump of the expected output or of the data you have that you want to convert? As if that is the data you want to convert, you already have a PHP array, if it is the expected output, can you provide the var_dump of the data you want to convert as well?

From what i read, cp, the input is a multi-line sting, in the format shown in the OP.

Take that STRING. (not an array object, an actual string. As if you’d screen-scraped the output of a print_r. and convert it into it’s array equivilant.

If i’ve missed something, let me know.

Okay, I’ll try playing around with a few regular expressions when I get home, that could hopefully simplify this process. As if you can write a regex that matches the key and the value of said key, you can then loop through the matches to create your associative array.

hmm, true… something like…

Array {.?(\[(.?)\] => (.?)
)
} ?

I came up with something similar

/\\[([a-zA-Z0-9]+)\\] => (.*?)(?:\\r\
|\\r|\
)/

When used in preg_match_all, the output should be

array (
  0 => 
  array (
    0 => '[plaintext] => This is an example CV. 
',
    1 => '[filename] => 1345550846.doc 
',
  ),
  1 => 
  array (
    0 => 'plaintext',
    1 => 'filename',
  ),
  2 => 
  array (
    0 => 'This is an example CV. ',
    1 => '1345550846.doc ',
  ),
)

I tried this code but it still doesn’t seem to be working (returns blank array).

Here is an example of the code i’m using:


$array_string = 'array(2) {
  ["plaintext"]=>
  string(23) "This is an example CV.
"
  ["filename"]=>
  string(14) "1345727271.doc"
}';

preg_match_all("/\\[([a-zA-Z0-9]+)\\] => (.*?)(?:\\r\
|\\r|\
)/", $array_string, $matches);

print_r($matches);

Your updated code has quotes around the array keys, I didn’t see that in the original text you posted. In fact, that is entirely different than the original text you posted, so which text do you have to convert?

The text is the same as post #5 http://www.sitepoint.com/forums/showthread.php?875714-Convert-array-string-into-an-array&p=5172303&viewfull=1#post5172303

Only the .doc name has changed slightly.

Just where are you getting this “array string”…and how do you know it is not actually an array? And just maybe, you can help yourself instead of being spoon fed like…[omitted]. Most of the code to convert the “array string” with regex is provided. Now help yourself for a change and make the necessary edits to solve the issue on Post #11.

Yes I know I could be nicer…
But those that don’t help themselves just aggravate me to no end.

I’m getting it from another server, I’m returning it as a string (using print_r) as it’s not possible to return as an array to the browser.

why not use json_encode and then json_decode on the receiving server?

Yes that’s probably a better solution actually.

So on the other server i use echo json_encode($array);

Then how would I convert back to an array? Just json_decode($string)?

The output of that is:

stdClass Object ( [plaintext] => This is an example CV. [filename] => 1345730220.doc )

right and you would feed that straight to json_decode() which will give you your array back.

Thanks, but it’s outputting the following:

stdClass Object
(
    [plaintext] => This is an example CV.
    [filename] => 1345730220.doc
)