3

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],
            ];}
21
  • 2
    In English por favor Commented Nov 13, 2018 at 18:05
  • 2
    @RiggsFolly OP edited. I voted to reopen. Commented Nov 13, 2018 at 18:21
  • 1
    Your array as been created like this { 1 : { data... } } ? Commented Nov 13, 2018 at 18:43
  • 1
    Please, put how it was been created on json_encode. Commented Nov 13, 2018 at 18:48
  • 1
    I know what is the problem, but you must accept my edit on your post first. Commented Nov 13, 2018 at 18:56

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)
It wasn't on my own, @Roger Russel made all the job
One is glad to be of service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.