/    Sign up×
Community /Pin to ProfileBookmark

Appending an Array

Hi,

I want to grab data from a .csv file and add all the data to the array. I have the part where I’m grabbing the data from the .csv file but the array issue I haven’t totally figured out.

Currently, I’m not adding all the data just the last row of data to the array and need help trying to add all the data to the array. Can someone please help me accomplish this. This is what I have:

[code=php]
global $info;
ini_set(“auto_detect_line_endings”, 1);
$current_row = 1;
$handle = fopen(“adduser.csv”, “r”);
$info=””;
while ( ($data = fgetcsv($handle, 10000, “,”) ) !== FALSE )
{
$number_of_fields = count($data);
if ($current_row == 1)
{
//Header line
for ($c=0; $c < $number_of_fields; $c++)
{
$header_array[$c] = $data[$c];
}
}
else
{
//Data line
for ($c=0; $c < $number_of_fields; $c++)
{
$data_array[$header_array[$c]] = $data[$c];
}
print_r($data_array);
$info = $data_array;
}
$current_row++;
}
//buildBatchXML($data_array);

echo “<br><br>This is new data:<br><br>”;
foreach($info as $key => $value)
{
echo “$key – $value<br>”;
}

fclose($handle);

?>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiFeb 17.2008 — Try replacing
[code=php]
$data_array[$header_array[$c]] = $data[$c];
[/code]

with
[code=php]
$data_array[][$header_array[$c]] = $data[$c];
[/code]

and see what it does ?
Copy linkTweet thisAlerts:
@NogDogFeb 17.2008 — [code=php]
$headers = array();
$info = array();
$current_row = 0;
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
if ($current_row++ == 0)
{
//Header line
foreach ($row as $value)
{
$headers[] = $value;
}
}
else
{
//Data line
foreach ($row as $key => $value)
{
$info[$counter][$headers[$key]] = $value;
}
}
}
[/code]

This will result in $info being a 2-dimension array. You would then reference a particular value as $info[3]['field name'] where "3" is the desired row number and 'field name' is the applicable column name from the header row.
Copy linkTweet thisAlerts:
@ZnupiFeb 17.2008 — Uhm.. check the comment:
[code=php]
$headers = array();
$info = array();
$current_row = 0;
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
if ($current_row++ == 0)
{
//Header line
foreach ($row as $value)
{
$headers[] = $value;
}
}
else
{
//Data line
foreach ($row as $key => $value)
{
$info[$counter][$headers[$key]] = $value;
// Don't you mean $counter_row? And you don't seem to increment that anywhere, won't it just store the last line in $info[1] ??
}
}
}
[/code]
[/QUOTE]


EDIT: SORRY, I can now see where you increment $counter_row. but still, shouldn't it be $counter_row there?
Copy linkTweet thisAlerts:
@NogDogFeb 17.2008 — Yeah, I changed the variable name at some point and missed all its uses:
[code=php]
$headers = array();
$info = array();
$current_row = 0;
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
if ($current_row++ == 0)
{
//Header line
foreach ($row as $value)
{
$headers[] = $value;
}
}
else
{
//Data line
foreach ($row as $key => $value)
{
$info[$current_row][$headers[$key]] = $value;
}
}
}
[/code]
×

Success!

Help @misslilbit02 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...