James C
James C

Reputation: 141

save array as .json then decode back to array

I know there are a lot of answers about saving a php array as a .json file but none of them seem to solve my problem.

Basically I have an array that is storing my data. first I check to see if a .json file has already been created. (each .json file is named using an $id variable)

If there is already a file with that id I want to update it and add more data that I get from my second array $newar

else create the file and add the data from $array.

Everything works apart from the format of the json file. here is the code below.

$array = array(

    array(

        'First Name' => $scan['FirstName'],
        'Last Name' => $scan['LastName'],
        'Email' => $scan['Email'],
        'Barcode' => $scan['Barcode'],
        'Phone' => $scan['Phone'],
        'Company' => $scan['Company'],
        'Position' => $scan['Position'],
        )

);

if (file_exists('client/jsonstore/'.$id.'.json')) {    
    $json = file_get_contents('client/jsonstore/'.$id.'.json');
    $json_data = json_decode($json,true);

    $newar = array(

        array(

        'First Name' => $scan['FirstName'],
        'Last Name' => $scan['LastName'],
        'Email' => $scan['Email'],
        'Barcode' => $scan['Barcode'],
        'Phone' => $scan['Phone'],
        'Company' => $scan['Company'],
        'Position' => $scan['Position'],
            )

    );

    array_push($json_data, $newar);

    $json = json_encode($json_data,JSON_PRETTY_PRINT);

    file_put_contents('client/jsonstore/'.$id.'.json', $json);

} else {
    //Encode the array into a JSON string.
    $encodedString = json_encode($array,JSON_PRETTY_PRINT);

    //Save the JSON string to a text file.
    file_put_contents('client/jsonstore/'.$id.'.json', $encodedString);

    //Retrieve the data from our text file.

}

when adding the first set of data my ,json file looks like this :

[
{
    "First Name": "first name",
    "Last Name": "last name",
    "Email": "[email protected]",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
}
]

When adding the next set of data to same file my .json looks like this :

[
{
    "First Name": "first name",
    "Last Name": "last name",
    "Email": "[email protected]",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
},
[
    {
        "First Name": "first name",
    "Last Name": "last name",
    "Email": "[email protected]",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
    }
]
]

and a var dump looks like this :

0 => 
array (size=7)
  'First Name' => string 'first name'      
  'Last Name' => string 'last name'
  'Email' => string '[email protected]'
  'Barcode' => string '732538896913809762001'
  'Phone' => string '00000000'
  'Company' => string 'company'
  'Position' => string 'position'
1 => 
array (size=1)
  0 => 
    array (size=7)
      'First Name' => string 'first name'      
      'Last Name' => string 'last name'
      'Email' => string '[email protected]'
      'Barcode' => string '732538896913809762001'
      'Phone' => string '00000000'
      'Company' => string 'company'
      'Position' => string 'position'

Upvotes: 1

Views: 126

Answers (3)

Noémie Kerroux
Noémie Kerroux

Reputation: 350

You set your new array as an array of arrays, that's why it's doing so, because you have a first array with an array in it, and you add a double array to it.

try replacing it by :

    $newar = array(
        'First Name' => $scan['FirstName'],
        'Last Name' => $scan['LastName'],
        'Email' => $scan['Email'],
        'Barcode' => $scan['Barcode'],
        'Phone' => $scan['Phone'],
        'Company' => $scan['Company'],
        'Position' => $scan['Position'],
    );

    array_push($json_data, $newar);

Upvotes: 1

James C
James C

Reputation: 141

Ive actually fixed this for anyone else who may have the problem.

$array = array(

array( // I kept this nested arary

'First Name' => $scan['FirstName'],
'Last Name' => $scan['LastName'],
'Email' => $scan['Email'],
'Barcode' => $scan['Barcode'],
'Phone' => $scan['Phone'],
'Company' => $scan['Company'],
'Position' => $scan['Position'],
)

);

if (file_exists('client/jsonstore/'.$id.'.json')) {    
$json = file_get_contents('client/jsonstore/'.$id.'.json');
$json_data = json_decode($json,true);

$newar = array(

 // I removed this inner array or nested array array( 
'First Name' => $scan['FirstName'],
'Last Name' => $scan['LastName'],
'Email' => $scan['Email'],
 'Barcode' => $scan['Barcode'],
'Phone' => $scan['Phone'],
'Company' => $scan['Company'],
'Position' => $scan['Position'],
 //)

 );

Upvotes: 3

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

The initial array is in following format:

[[],[],[]]

if you want to push to it, you want to push just:

$newar = array(
    'First Name' => $scan['FirstName'],
    'Last Name' => $scan['LastName'],
    'Email' => $scan['Email'],
    'Barcode' => $scan['Barcode'],
    'Phone' => $scan['Phone'],
    'Company' => $scan['Company'],
    'Position' => $scan['Position'],
);

If you change the above definition, your code will work.

Upvotes: 4

Related Questions