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;
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
add a comment |
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
php.net/manual/en/function.str-getcsv.php#114764
– Script47
Mar 25 at 12:34
Your results seem to suggest that theexplode()call isn't doing anything useful and could be removed.
– Nigel Ren
Mar 25 at 13:05
add a comment |
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
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
php
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 theexplode()call isn't doing anything useful and could be removed.
– Nigel Ren
Mar 25 at 13:05
add a comment |
php.net/manual/en/function.str-getcsv.php#114764
– Script47
Mar 25 at 12:34
Your results seem to suggest that theexplode()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
add a comment |
2 Answers
2
active
oldest
votes
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);
Thanks a lot. This solves my problem :)
– RK251
Mar 26 at 4:04
add a comment |
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))
);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
Thanks a lot. This solves my problem :)
– RK251
Mar 26 at 4:04
add a comment |
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);
Thanks a lot. This solves my problem :)
– RK251
Mar 26 at 4:04
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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))
);
add a comment |
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))
);
add a comment |
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))
);
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))
);
answered Mar 25 at 13:23
AbraCadaverAbraCadaver
61.1k7 gold badges42 silver badges68 bronze badges
61.1k7 gold badges42 silver badges68 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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