skip to Main Content

I’m trying to json_decode a JSON package from the database and then explode one of the properties by ‘n’ as follows. I’m in laravel so I’ve dd’d all the variables as they go through the process

 $options = json_decode($field['options'],true);
 $rows = explode('n', $options['choices']);
 dd($field['options'],$options,$rows,'here');

See the screenshot of the printed results. $field[‘options’] looks like a good JSON string.

When decoded, $options has extra quote marks added.

Exploding the weird string doesn’t work.

See the screenshot here

Tried to JSON decode an object and returned extra quote marks in the string, now I can’t explode the string.

3

Answers


  1. Chosen as BEST ANSWER

    OK so the extra quote marks were a bit of a curve-ball...

    There was nothing wrong with the json_decode as @pickuse mentioned.

    The issue was with the explode. Instead of using 'n' as the delimiter, you should use PHP_EOL as follows:

    $rows = explode('n', $options['choices']);
    

  2. This may help, but there would be a problem that I just noticed after submitting this the first time.
    Mine reads a file so I needed to trim all the returns, trim($line) and also I stripped out all n, str_replace("n",”,$line).

    This will likely to cause a problem with array:2[choices => and Array:1 [0 => as the n is the delimiter. So you’re going to need to encapsulate those values.

    This is what I wrote a while back to turn json into yaml. I trim each line, contact the string then strip all new lines "n" and then decode the long json string.

    <?
    $file='rewbin-test.json';
    $handle = fopen($file,'r');
    $data='';   ###>  This is going to be a long string no line breaks. 
                ###> I think it should be your $field['options'] value.
    $dec= array ();  ###>  This array I create
    //$records=array();
    while($line = fgets($handle)){
        $data.=trim($line); ###> This representing your $field['options'] value.
    }
    fclose($handle);
                //echo $data;
    $data=str_replace("n",'',$data);
                echo $data; ###>  long string no-returns and no "n"
    
    $dec=json_decode($data, true);
                print_r($dec);
        ###> this is multi-dimension array I think you're looking for.
    
    /*
    $yaml=var_dump(yaml_emit($dec));  ###> convert array to yaml.
    print $yaml;            
    */
    ?>
    

    10 lines from the rewbin-test.json file. In the file there are returns.

    {
      "canIpForward": false,
      "confidentialInstanceConfig": {
        "enableConfidentialCompute": false
      },
      "deletionProtection": false,
      "description": "",
      "disks": [
        {
          "autoDelete": true,
    

    In the $data var there are no returns.

    That portion in the array.

    Array
    (
        [canIpForward] =>
        [confidentialInstanceConfig] => Array
            (
                [enableConfidentialCompute] =>
            )
    
        [deletionProtection] =>
        [description] =>
        [disks] => Array
            (
                [0] => Array
                    (
                        [autoDelete] => 1
                        [boot] => 1
    

    Hope this is helpful.
    rew

    Login or Signup to reply.
  3. Your problem is using the single quote for the n, it will NOT interpret to newline, you need using double quote.

    Reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

    No problems with my code to extract the multiline data

    $dataJson = '{"choices": "council| Council taxnelectric|Electricitynwifi/WiFinwater|Waterngas|Gas"}';
    $dataArray = json_decode($dataJson, true);
    var_export( explode("n", $dataArray['choices']) );
    

    Output:

    array (
      0 => 'council| Council tax',
      1 => 'electric|Electricity',
      2 => 'wifi/WiFi',
      3 => 'water|Water',
      4 => 'gas|Gas',
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search