douou1872 2018-11-13 18:05
浏览 81
已采纳

将Array Php转换为JSON的错误

I am having some troubles to convert a multidimensional PHP Array to JSON. I convert it with json_encode, but it gets null.

I am trying to develop an orgChart, the data is read from an CSV file and saved in an array. The layout and JS code is built to receive a JSON file, so I need it in that format.

This is an slice of the array, it contains 175 arrays within

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
        )

)

I am using json_encode to convert the array to JSON OBS: *** $colab is the array's name fed by the CSV

$dados_json = json_encode($colab);
$fp = fopen("jsonOrgan.json", "w");
$write = fwrite($fp, $dados_json);
fclose($fp);

I need it to output on JSON as follow:

[{
    "id": 1,
    "cargo": "ELOTECH",
    "nome": "",
    "idcargo": 1,
    "pai": 0
}]

But it return null

Here is how I create the array from the CSV file.

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],
            ];}
  • 写回答

1条回答 默认 最新

  • douyueju2912 2018-11-13 19:30
    关注

    With Roger Russel's instructions, my problem got solved.

    The JSON was getting NULL because of the encoding. I used the uft8_encode and solved this problem. Then i changed the array creation to fit the pattern that I need the JSON file to be.

    I was creating the Array Using a counter as index, as follow:

    $colab[$linha] = [
                    'id' => $line[0],
                    'nome' => $line[1],
                    'cargo' => $line[4],
                    'idcargo' => $line[0],
                    'pai' => $line[5],
                ]
    

    Then changed it to be created without passing the index:

    $colab[] = [
                        'id' => $line[0],
                        'nome' => $line[1],
                        'cargo' => $line[4],
                        'idcargo' => $line[0],
                        'pai' => $line[5],
                    ]
    

    And that's it, my problem was solved!

    Thank you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题