1

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'
2
  • What is wrong with the format. Those are valid json. Commented Mar 1, 2018 at 13:44
  • Both $array and $newarr in your code are arrays of arrays. And probably array_push() (a function that is over-used in wrong contexts) doesn't do what you think. Or it does and in this case $newarr must not be a 2-level array. P.S. If you don't have code that uses array_pop() then you should not be using array_push(). Not because of PHP but because of the logic of your program. Commented Mar 1, 2018 at 13:49

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I did figure it out just after posting see my own answer below
3

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'],
 //)

 );

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.