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

问题描述 投票:0回答:3

我知道有很多关于将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": "[email protected]",
    "Barcode": "732538896913809762001",
    "Phone": "00000000",
    "Company": "company",
    "Position": "position"
}
]

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

[
{
    "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"
    }
]
]

var转储如下所示:

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'
php arrays json
3个回答
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'],
);

如果您更改上述定义,您的代码将起作用。


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

 );

1
投票

您将新数组设置为数组数组,这就是为什么它正在这样做,因为您有第一个数组,其中包含一个数组,并为其添加一个双数组。

尝试将其替换为:

    $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);
© www.soinside.com 2019 - 2024. All rights reserved.