Convert array string into an array

Help me understand what you mean it is outputting the following?

<?php
$myArray = array('plaintext' => 'This is an example CV.', 'filename' => '1345730220.doc');
$jsonString = json_encode($myArray);
$decodedArray = json_decode($jsonString, true); // Note: I pass true for the $assoc optional parameter

var_dump($myArray, $decodedArray); // Should be nearly identical
echo $decodedArray['plaintext'];
?>

Your output to json_encode should look like:

{"plaintext":"This is an example CV.","filename":"1345730220.doc"}

Maybe it’s because I’m fetching the output from the remote server using CURL? I think it’s seeing it as a string?

It is a string, are you setting the optional parameter in json_decode for $assoc to true?

$decodedArray = json_decode($jsonString, true); // Note: I pass true for the $assoc optional parameter

No, should I be?

If you want an associative array after decoding the json_encode output, then yes. Otherwise, you end up with an object and have to access your values like so: $decodedArray->plaintext and $decodedArray->filename versus, when using true in the optional parameter, you can use it as $decodedArray[‘plaintext’] and $decodedArray[‘filename’]

Instead of using json_encode()/json_decode(), use serialize()/unserialize(). The latter allows you to do things like transfer PHP objects intact across hosts while JSON is more limited. If you are transferring data from a database, I recommend doing:

echo base64_encode(serialize($row)) . "\
";

On the source and this:

$lines = explode("\
", file_get_contents("sourceurlhere"));
foreach ($lines as $line)
{
  $row = unserialize(base64_decode(trim($line)));
...
}

For the destination. That’s the fastest, easiest way to get data to cross from one PHP host to another on a temporary basis (e.g. a data migration export/import script set for a redesigned web app). You wouldn’t want to do this sort of thing for anything other than a quick-n-dirty script that will be used just once or twice - otherwise you’ll create a maintenance nightmare in short order.