我在將多維PHP數組轉換為JSON時遇到了一些麻煩.我使用json_encode進行了轉換,但它為null.
我正在嘗試開發orgChart,數據是從CSV文件中讀取的,并保存在數組中.布局和JS代碼用于接收JSON文件,因此我需要使用這種格式.
這是數組的一部分,其中包含175個數組
Array
(
[2] => Array
(
[id] => 1
[nome] => ELOTECH
[cargo] => ""
[idcargo] => 1
[pai] => 0
)
[3] => Array
(
[id] => 10
[nome] => Departamento Pessoal
[cargo] =>
[idcargo] => 10
[pai] => 1
)
[4] => Array
(
[id] => 20
[nome] => Comercial
[cargo] =>
[idcargo] => 20
[pai] => 1
)
)
我正在使用json_encode將數組轉換為JSON
OBS:*** $colab是CSV饋送的陣列名稱
$dados_json = json_encode($colab);
$fp = fopen("jsonOrgan.json", "w");
$write = fwrite($fp, $dados_json);
fclose($fp);
我需要它在JSON上輸出,如下所示:
[{
"id": 1,
"cargo": "ELOTECH",
"nome": "",
"idcargo": 1,
"pai": 0
}]
但它返回null
這是我從CSV文件創建數組的方法.
while ($line = fgetcsv($save, 1000, ";")) {
if ($linha++ == 0) {
continue;
}
$colab[$linha] = [
'id' => $line[0],
'nome' => $line[1],
'cargo' => $line[4],
'idcargo' => $line[0],
'pai' => $line[5],
];}