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!

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?