0

My error log has sent me an email which includes a big array of data, which was stored in the users session. This array SHOULD have been converted to a JSON encoded string and stored in my database but something has gone wrong. If I have the data as a text string, what is the quickest way to convert back to JSON and save back to my database. Here is an example of SOME of the data from the array. Remember: I have this as a text string an en email, not as an actual array. The content in the email has the <pre> tags surrounding it.

            [customer_firstname] => James
            [customer_lastname] => Smith
            [customer_address1] => 
            [customer_postcode] => AB12CD
            [customer_address2] => 
            [customer_address3] => 
            [customer_address4] => 
            [customer_town] => London
            [customer_county] => 
            [customer_country] => UK
James Wilson
  • 809
  • 3
  • 14
  • 25
  • @MagnusEriksson You can't encode a textual representation of an array. – jhmckimm Oct 05 '16 at 15:25
  • I take it that's PHP `print_r` formatting? There's no specified way to convert that to anything. You *may* be able to get your data from it, but the format doesn't take any precaution with special characters and can be ambiguous to parse, hence is not suitable for automatic processing. – deceze Oct 05 '16 at 15:26
  • @MagnusEriksson The question clearly states that the intention was to have the array JSON encoded, but that something had gone wrong. – jhmckimm Oct 05 '16 at 15:27

1 Answers1

1

The simplest way I can think of is a simple str_replace on the output, converting it into an array that you can copy-paste into a PHP script and then convert to JSON.

<?php
  $errorInfo = "[customer_firstname] => James
        [customer_lastname] => Smith
        [customer_address1] => 
        [customer_postcode] => AB12CD
        [customer_address2] => 
        [customer_address3] => 
        [customer_address4] => 
        [customer_town] => London
        [customer_county] => 
        [customer_country] => UK";

  echo str_replace(["[", "]", "=> ", "\n"], ["'", "'", "=>\"", "\",\n<br />"],  $errorInfo);
?>

Outputs:

'customer_firstname' =>"James", 
'customer_lastname' =>"Smith", 
'customer_address1' =>"", 
'customer_postcode' =>"AB12CD", 
'customer_address2' =>"", 
'customer_address3' =>"", 
'customer_address4' =>"", 
'customer_town' =>"London", 
'customer_county' =>"", 
'customer_country' =>"UK

Note that the last closing quote is missed... Need to add this yourself or add it after the str_replace

Then add braces and assign:

$error = [
  'customer_firstname' =>"James", 
  'customer_lastname' =>"Smith", 
  'customer_address1' =>"", 
  'customer_postcode' =>"AB12CD", 
  'customer_address2' =>"", 
  'customer_address3' =>"", 
  'customer_address4' =>"", 
  'customer_town' =>"London", 
  'customer_county' =>"", 
  'customer_country' =>"UK"];

Simplez:

echo json_encode($error);
jhmckimm
  • 870
  • 6
  • 15
  • This is a little naive in that it will blow up on, say, `[customer_whatever] => foor"bar`. Any presence of `'`, `"`, `[` or `]` in the data is likely to break it. – user229044 Oct 05 '16 at 15:57
  • @meagar Yes, that's correct. Regex would get around that, but I'm no regex pro so I stuck with simple. – jhmckimm Oct 05 '16 at 16:16
  • Something like [this](http://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable) would be ideal... As you can see, however, it's highly complicated. – jhmckimm Oct 05 '16 at 16:18