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],
];}