php数组转为js json,javascript-将数组php转换为JSON时出错

我在将多维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],

];}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值