Errors converting Array Php to JSON












2















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









share|improve this question




















  • 2





    In English por favor

    – RiggsFolly
    Nov 13 '18 at 18:05






  • 2





    @RiggsFolly OP edited. I voted to reopen.

    – Funk Forty Niner
    Nov 13 '18 at 18:21






  • 1





    Your array as been created like this { 1 : { data... } } ?

    – Roger Russel
    Nov 13 '18 at 18:43






  • 1





    Please, put how it was been created on json_encode.

    – Roger Russel
    Nov 13 '18 at 18:48






  • 1





    I know what is the problem, but you must accept my edit on your post first.

    – Roger Russel
    Nov 13 '18 at 18:56
















2















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









share|improve this question




















  • 2





    In English por favor

    – RiggsFolly
    Nov 13 '18 at 18:05






  • 2





    @RiggsFolly OP edited. I voted to reopen.

    – Funk Forty Niner
    Nov 13 '18 at 18:21






  • 1





    Your array as been created like this { 1 : { data... } } ?

    – Roger Russel
    Nov 13 '18 at 18:43






  • 1





    Please, put how it was been created on json_encode.

    – Roger Russel
    Nov 13 '18 at 18:48






  • 1





    I know what is the problem, but you must accept my edit on your post first.

    – Roger Russel
    Nov 13 '18 at 18:56














2












2








2


1






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









share|improve this question
















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






javascript php arrays json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 19:15







Jean Lima

















asked Nov 13 '18 at 18:05









Jean LimaJean Lima

518




518








  • 2





    In English por favor

    – RiggsFolly
    Nov 13 '18 at 18:05






  • 2





    @RiggsFolly OP edited. I voted to reopen.

    – Funk Forty Niner
    Nov 13 '18 at 18:21






  • 1





    Your array as been created like this { 1 : { data... } } ?

    – Roger Russel
    Nov 13 '18 at 18:43






  • 1





    Please, put how it was been created on json_encode.

    – Roger Russel
    Nov 13 '18 at 18:48






  • 1





    I know what is the problem, but you must accept my edit on your post first.

    – Roger Russel
    Nov 13 '18 at 18:56














  • 2





    In English por favor

    – RiggsFolly
    Nov 13 '18 at 18:05






  • 2





    @RiggsFolly OP edited. I voted to reopen.

    – Funk Forty Niner
    Nov 13 '18 at 18:21






  • 1





    Your array as been created like this { 1 : { data... } } ?

    – Roger Russel
    Nov 13 '18 at 18:43






  • 1





    Please, put how it was been created on json_encode.

    – Roger Russel
    Nov 13 '18 at 18:48






  • 1





    I know what is the problem, but you must accept my edit on your post first.

    – Roger Russel
    Nov 13 '18 at 18:56








2




2





In English por favor

– RiggsFolly
Nov 13 '18 at 18:05





In English por favor

– RiggsFolly
Nov 13 '18 at 18:05




2




2





@RiggsFolly OP edited. I voted to reopen.

– Funk Forty Niner
Nov 13 '18 at 18:21





@RiggsFolly OP edited. I voted to reopen.

– Funk Forty Niner
Nov 13 '18 at 18:21




1




1





Your array as been created like this { 1 : { data... } } ?

– Roger Russel
Nov 13 '18 at 18:43





Your array as been created like this { 1 : { data... } } ?

– Roger Russel
Nov 13 '18 at 18:43




1




1





Please, put how it was been created on json_encode.

– Roger Russel
Nov 13 '18 at 18:48





Please, put how it was been created on json_encode.

– Roger Russel
Nov 13 '18 at 18:48




1




1





I know what is the problem, but you must accept my edit on your post first.

– Roger Russel
Nov 13 '18 at 18:56





I know what is the problem, but you must accept my edit on your post first.

– Roger Russel
Nov 13 '18 at 18:56












1 Answer
1






active

oldest

votes


















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!






share|improve this answer


























  • Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

    – Motassem MK
    Nov 13 '18 at 19:32






  • 1





    It wasn't on my own, @Roger Russel made all the job

    – Jean Lima
    Nov 13 '18 at 19:34











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287037%2ferrors-converting-array-php-to-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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!






share|improve this answer


























  • Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

    – Motassem MK
    Nov 13 '18 at 19:32






  • 1





    It wasn't on my own, @Roger Russel made all the job

    – Jean Lima
    Nov 13 '18 at 19:34
















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!






share|improve this answer


























  • Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

    – Motassem MK
    Nov 13 '18 at 19:32






  • 1





    It wasn't on my own, @Roger Russel made all the job

    – Jean Lima
    Nov 13 '18 at 19:34














3












3








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!






share|improve this answer















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!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 19:35

























answered Nov 13 '18 at 19:30









Jean LimaJean Lima

518




518













  • Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

    – Motassem MK
    Nov 13 '18 at 19:32






  • 1





    It wasn't on my own, @Roger Russel made all the job

    – Jean Lima
    Nov 13 '18 at 19:34



















  • Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

    – Motassem MK
    Nov 13 '18 at 19:32






  • 1





    It wasn't on my own, @Roger Russel made all the job

    – Jean Lima
    Nov 13 '18 at 19:34

















Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

– Motassem MK
Nov 13 '18 at 19:32





Good job finding the problem and solution on your own then sharing it for others, keep up the good work :)

– Motassem MK
Nov 13 '18 at 19:32




1




1





It wasn't on my own, @Roger Russel made all the job

– Jean Lima
Nov 13 '18 at 19:34





It wasn't on my own, @Roger Russel made all the job

– Jean Lima
Nov 13 '18 at 19:34


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287037%2ferrors-converting-array-php-to-json%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values