我在将多维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],
];}
如果觉得《php数组转为js json javascript-将数组php转换为JSON时出错》对你有帮助,请点赞、收藏,并留下你的观点哦!