Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent json_encode($multidimensaional_assoc_array) from adding numeric keys?

I have a php script that takes a json file, decodes it $mda = json_decode(file_get_contents("file.json"),true);, edits some parts of it, and then encodes it back json_encode($mda);

Here’s the script I’m using:

PHP

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function edit_medication($data)
{
    extract($_COOKIE);
    $root = "../../userdata/$family_username";
    $family = json_decode(file_get_contents("$root/household.json"), true);
    $data['status_epoch'] = strtotime("Now");

    $medications = json_decode(file_get_contents("$root/$username.json"), true);
    print_r($medications['medications']);
    for ($x = 0; $x < count($medications); $x++) {
        if ($medications['medications'][$x]['name'] === $data['name']) {
            unset($medications['medications'][$x]);
        }
    }
    array_push($medications['medications'], $data);
    print_r($medications['medications']);
    file_put_contents("$root/$username.json", json_encode($medications, JSON_FORCE_OBJECT));
    return;
}

Then the JSON file

{
  "medications": [
    {
      "name": "Adderall XR",
      "alternative_name": "Dextroamphatemine",
      "dosage": "30 mg",
      "instructions": "Take 2 qAM",
      "prescriber": "Yakov",
      "department": "Psychiatry",
      "notes": "",
      "status": "Active",
      "status_epoch": 1667938912
    },
    {
      "name": "Duloxetine",
      "alternative_name": "Cymbalta",
      "dosage": "60 mg",
      "instructions": "Take 1 qAM",
      "prescriber": "Yakov",
      "department": "Psychiatry",
      "notes": "1667938912",
      "status": "Active",
      "status_epoch": 1668026537
    }
  ],
  "doagnises": [
    {
      "name": "Autism Spectrum Disorder",
      "simple_name": "Autism",
      "managing_provider": "Poling",
      "department": "Pediatrics",
      "medicated": "No"
    }
  ]
}

When I run it though the function, it updates the file to show as such:

print_r($medications); [As shown in the console.log]

Data: Array
(
    [0] => Array
        (
            [name] => Adderall XR
            [alternative_name] => Dextroamphatemine
            [dosage] => 30 mg
            [instructions] => Take 2 qAM
            [prescriber] => Yakov
            [department] => Psychiatry
            [notes] => 
            [status] => Active
            [status_epoch] => 1667938912
        )

    [1] => Array
        (
            [name] => Duloxetine
            [alternative_name] => Cymbalta
            [dosage] => 60 mg
            [instructions] => Take 1 qAM
            [prescriber] => Yakov
            [department] => Psychiatry
            [notes] => 1667938912
            [status] => Active
            [status_epoch] => 1668026537
        )

)
Array
(
    [0] => Array
        (
            [name] => Adderall XR
            [alternative_name] => Dextroamphatemine
            [dosage] => 30 mg
            [instructions] => Take 2 qAM
            [prescriber] => Yakov
            [department] => Psychiatry
            [notes] => 
            [status] => Active
            [status_epoch] => 1667938912
        )

    [2] => Array
        (
            [name] => Duloxetine
            [alternative_name] => Cymbalta
            [dosage] => 60 mg
            [instructions] => Take 1 qAM
            [prescriber] => Yakov
            [department] => Psychiatry
            [notes] => 1667938912
            [status] => Active
            [status_epoch] => 1668027846
        )
)
{
  "medications": {
    "0": {                <------IT ADDED THIS--------|
      "name": "Adderall XR",
      "alternative_name": "Dextroamphatemine",
      "dosage": "30 mg",
      "instructions": "Take 2 qAM",
      "prescriber": "Yakov",
      "department": "Psychiatry",
      "notes": "",
      "status": "Active",
      "status_epoch": 1667938912
    },
    "2": {
      "name": "Duloxetine",
      "alternative_name": "Cymbalta",
      "dosage": "60 mg",
      "instructions": "Take 1 qAM",
      "prescriber": "Yakov",
      "department": "Psychiatry",
      "notes": "1667938912",
      "status": "Active",
      "status_epoch": 1668027846
    }
  },
  "doagnises": {
    "0": {
      "name": "Autism Spectrum Disorder",
      "simple_name": "Autism",
      "managing_provider": "Poling",
      "department": "Pediatrics",
      "medicated": "No"
    }
  }
}

How do I get it to NOT add the numeric keys?

I’ve tried doing json_encode($medications, JSON_FORCE_OBJECT) and that didn’t do anything different.

>Solution :

For json_encode() to encode a list instead of an object the array keys must be numeric, sequential, and without gaps. You code is unsetting items in the array, which will therefore have gaps.

To fix this add:

$medications['medications'] = array_values($medications['medications']);

after the loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading