将数组保存为.json然后解码回数组

时间:2018-03-01 13:39:34

标签: php arrays json

我知道有很多关于将php数组保存为.json文件的答案,但它们似乎都无法解决我的问题。

基本上我有一个存储数据的数组。首先,我检查一下.json文件是否已经创建。 (每个.json文件使用$ id变量命名)

如果已经存在具有该ID的文件,我想更新它并添加我从第二个数组获取的更多数据$ newar

否则创建文件并从$ array中添加数据。

一切都与json文件的格式不同。这是下面的代码。

$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.

}

添加第一组数据时,json文件如下所示:

[
{
    "First Name": "first name",
    "Last Name": "last name",
    "Email": "mail@mail.com",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
}
]

将下一组数据添加到同一文件时,我的.json如下所示:

[
{
    "First Name": "first name",
    "Last Name": "last name",
    "Email": "mail@mail.com",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
},
[
    {
        "First Name": "first name",
    "Last Name": "last name",
    "Email": "mail@mail.com",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
    }
]
]

并且var转储如下所示:

0 => 
array (size=7)
  'First Name' => string 'first name'      
  'Last Name' => string 'last name'
  'Email' => string 'mail@mail.com'
  '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 'mail@mail.com'
      'Barcode' => string '732538896913809762001'
      'Phone' => string '00000000'
      'Company' => string 'company'
      'Position' => string 'position'

3 个答案:

答案 0 :(得分:3)

初始数组采用以下格式:

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

如果你想推进它,你想推送:

MultiIndex

如果您更改上述定义,您的代码就可以使用。

答案 1 :(得分:2)

我已经为其他可能遇到问题的人解决了这个问题。

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

 );

答案 2 :(得分:1)

您将新数组设置为数组数组,这就是它为什么这样做的原因,因为您有第一个包含数组的数组,并为其添加了一个双数组。 / p>

尝试将其替换为:

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