Read data from CSV into a single array or a single jsonDeleting an element from an array in PHPReturning JSON from a PHP Scriptphp array to csv download integrated into my current script not working properlyFormat CSV into json to be consumed by ui.autocompleteHow can I put certain rows from CSV file into JSON arrays within a JSON object? (PHP)Converting a single column from a CSV in to a simple array using PHPEmpty tag reading out CSV to arrayPHP convert json into csv with sub arrayRead next cell in same column CSV to XML using PHPErrors converting Array Php to JSON

Why won't the ground take my seed?

Why was Mal so quick to drop Bester in favour of Kaylee?

Do space suits measure "methane" levels or other biological gases?

Why isn’t the tax system continuous rather than bracketed?

What is the olden name for sideburns?

Are cosigner founder websites like hireacosigner.com, cosignerfinder.com etc. legit?

Can I ask to speak to my future colleagues before accepting an offer?

What does grep -v "grep" mean and do?

Is it bad to describe a character long after their introduction?

Generate and graph the Recamán Sequence

Procedurally generate regions on island

How can I convince my reader that I will not use a certain trope?

Do 3D printers really reach 50 micron (0.05 mm) accuracy?

Different budgets within roommate group

How was film developed in the late 1920s?

What is a macro? Difference between macro and function?

Can the passive "être + verbe" sometimes mean the past?

Why do I need two parameters in an HTTP parameter pollution attack?

Do I have to roll to maintain concentration if a target other than me who is affected by my concentration spell takes damage?

Needle Hotend for nonplanar printing

3D nonogram, beginner's edition

What's the safest way to inform a new user of their password on my web site?

Loss of majority in Westminster

Are metaheuristics ever practical for continuous optimization?



Read data from CSV into a single array or a single json


Deleting an element from an array in PHPReturning JSON from a PHP Scriptphp array to csv download integrated into my current script not working properlyFormat CSV into json to be consumed by ui.autocompleteHow can I put certain rows from CSV file into JSON arrays within a JSON object? (PHP)Converting a single column from a CSV in to a simple array using PHPEmpty tag reading out CSV to arrayPHP convert json into csv with sub arrayRead next cell in same column CSV to XML using PHPErrors converting Array Php to JSON






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am using jQuery autocomplete function. For it to work I need the json in this format ["Alaska","Alabama","Arkansas"] . Currently I am reading the data from a CSV file. This is my code.



$f = fopen($filename, "r");
while ($row = fgetcsv($f))
$out = $row[2];
print_r (explode(" , ",$out));



Now I get the output as



Array
(
[0] => Alaska
)
Array
(
[0] => Alabama
)
Array
(
[0] => Arkansas
)


And my Json as



["Alaska"]["Romeo"]["Arkansas"]


How do I convert the array into a simple array like



Array
(
[0] => Alaska
[1] => Alabama
[2] => Arkansas
)


If not into a json like this



["Alaska","Alabama","Arkansas"]









share|improve this question






















  • php.net/manual/en/function.str-getcsv.php#114764

    – Script47
    Mar 25 at 12:34











  • Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

    – Nigel Ren
    Mar 25 at 13:05

















0















I am using jQuery autocomplete function. For it to work I need the json in this format ["Alaska","Alabama","Arkansas"] . Currently I am reading the data from a CSV file. This is my code.



$f = fopen($filename, "r");
while ($row = fgetcsv($f))
$out = $row[2];
print_r (explode(" , ",$out));



Now I get the output as



Array
(
[0] => Alaska
)
Array
(
[0] => Alabama
)
Array
(
[0] => Arkansas
)


And my Json as



["Alaska"]["Romeo"]["Arkansas"]


How do I convert the array into a simple array like



Array
(
[0] => Alaska
[1] => Alabama
[2] => Arkansas
)


If not into a json like this



["Alaska","Alabama","Arkansas"]









share|improve this question






















  • php.net/manual/en/function.str-getcsv.php#114764

    – Script47
    Mar 25 at 12:34











  • Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

    – Nigel Ren
    Mar 25 at 13:05













0












0








0








I am using jQuery autocomplete function. For it to work I need the json in this format ["Alaska","Alabama","Arkansas"] . Currently I am reading the data from a CSV file. This is my code.



$f = fopen($filename, "r");
while ($row = fgetcsv($f))
$out = $row[2];
print_r (explode(" , ",$out));



Now I get the output as



Array
(
[0] => Alaska
)
Array
(
[0] => Alabama
)
Array
(
[0] => Arkansas
)


And my Json as



["Alaska"]["Romeo"]["Arkansas"]


How do I convert the array into a simple array like



Array
(
[0] => Alaska
[1] => Alabama
[2] => Arkansas
)


If not into a json like this



["Alaska","Alabama","Arkansas"]









share|improve this question














I am using jQuery autocomplete function. For it to work I need the json in this format ["Alaska","Alabama","Arkansas"] . Currently I am reading the data from a CSV file. This is my code.



$f = fopen($filename, "r");
while ($row = fgetcsv($f))
$out = $row[2];
print_r (explode(" , ",$out));



Now I get the output as



Array
(
[0] => Alaska
)
Array
(
[0] => Alabama
)
Array
(
[0] => Arkansas
)


And my Json as



["Alaska"]["Romeo"]["Arkansas"]


How do I convert the array into a simple array like



Array
(
[0] => Alaska
[1] => Alabama
[2] => Arkansas
)


If not into a json like this



["Alaska","Alabama","Arkansas"]






php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 at 12:31









RK251RK251

32 bronze badges




32 bronze badges












  • php.net/manual/en/function.str-getcsv.php#114764

    – Script47
    Mar 25 at 12:34











  • Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

    – Nigel Ren
    Mar 25 at 13:05

















  • php.net/manual/en/function.str-getcsv.php#114764

    – Script47
    Mar 25 at 12:34











  • Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

    – Nigel Ren
    Mar 25 at 13:05
















php.net/manual/en/function.str-getcsv.php#114764

– Script47
Mar 25 at 12:34





php.net/manual/en/function.str-getcsv.php#114764

– Script47
Mar 25 at 12:34













Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

– Nigel Ren
Mar 25 at 13:05





Your results seem to suggest that the explode() call isn't doing anything useful and could be removed.

– Nigel Ren
Mar 25 at 13:05












2 Answers
2






active

oldest

votes


















2














You build an array of values and then put them in json_encode() function.



$data = [];
$f = fopen($filename, "r");
while ($row = fgetcsv($f))
$out = $row[2];
$data = array_merge($data, explode(" , ",$out)));


echo json_encode($data);





share|improve this answer























  • Thanks a lot. This solves my problem :)

    – RK251
    Mar 26 at 4:04


















0














Might be easier to read the file into an arary, map to a function that explodes, then encode and display:



echo json_encode(
array_map(function($v) return explode(" , ", $v)[2]; , file($filename))
);





share|improve this answer

























    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%2f55337850%2fread-data-from-csv-into-a-single-array-or-a-single-json%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You build an array of values and then put them in json_encode() function.



    $data = [];
    $f = fopen($filename, "r");
    while ($row = fgetcsv($f))
    $out = $row[2];
    $data = array_merge($data, explode(" , ",$out)));


    echo json_encode($data);





    share|improve this answer























    • Thanks a lot. This solves my problem :)

      – RK251
      Mar 26 at 4:04















    2














    You build an array of values and then put them in json_encode() function.



    $data = [];
    $f = fopen($filename, "r");
    while ($row = fgetcsv($f))
    $out = $row[2];
    $data = array_merge($data, explode(" , ",$out)));


    echo json_encode($data);





    share|improve this answer























    • Thanks a lot. This solves my problem :)

      – RK251
      Mar 26 at 4:04













    2












    2








    2







    You build an array of values and then put them in json_encode() function.



    $data = [];
    $f = fopen($filename, "r");
    while ($row = fgetcsv($f))
    $out = $row[2];
    $data = array_merge($data, explode(" , ",$out)));


    echo json_encode($data);





    share|improve this answer













    You build an array of values and then put them in json_encode() function.



    $data = [];
    $f = fopen($filename, "r");
    while ($row = fgetcsv($f))
    $out = $row[2];
    $data = array_merge($data, explode(" , ",$out)));


    echo json_encode($data);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 25 at 12:33









    SergejSergej

    1,0076 silver badges14 bronze badges




    1,0076 silver badges14 bronze badges












    • Thanks a lot. This solves my problem :)

      – RK251
      Mar 26 at 4:04

















    • Thanks a lot. This solves my problem :)

      – RK251
      Mar 26 at 4:04
















    Thanks a lot. This solves my problem :)

    – RK251
    Mar 26 at 4:04





    Thanks a lot. This solves my problem :)

    – RK251
    Mar 26 at 4:04













    0














    Might be easier to read the file into an arary, map to a function that explodes, then encode and display:



    echo json_encode(
    array_map(function($v) return explode(" , ", $v)[2]; , file($filename))
    );





    share|improve this answer



























      0














      Might be easier to read the file into an arary, map to a function that explodes, then encode and display:



      echo json_encode(
      array_map(function($v) return explode(" , ", $v)[2]; , file($filename))
      );





      share|improve this answer

























        0












        0








        0







        Might be easier to read the file into an arary, map to a function that explodes, then encode and display:



        echo json_encode(
        array_map(function($v) return explode(" , ", $v)[2]; , file($filename))
        );





        share|improve this answer













        Might be easier to read the file into an arary, map to a function that explodes, then encode and display:



        echo json_encode(
        array_map(function($v) return explode(" , ", $v)[2]; , file($filename))
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 13:23









        AbraCadaverAbraCadaver

        61.1k7 gold badges42 silver badges68 bronze badges




        61.1k7 gold badges42 silver badges68 bronze badges



























            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%2f55337850%2fread-data-from-csv-into-a-single-array-or-a-single-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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해