Re: [PHP] file/array manipulation

This is a discussion on Re: [PHP] file/array manipulation within the PHP General forums, part of the PHP Programming Forums category; Ok I got this to work but I'm not sure how -- and am having trouble re-producing the data: ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-16-2005
Mignon Hunter
 
Posts: n/a
Default Re: [PHP] file/array manipulation

Ok I got this to work but I'm not sure how -- and am having trouble re-producing the data:

My script I have so far is:

$lines = file('test2.csv');
foreach ($lines as $line) {
$line_split = explode(",", $line);
if(!(empty($line_split[0]) ) ){
//echo "I'm not empty";
$new_array = array(
$line_split[0] => $line_split[1]
);
}
print("<pre>");
print_r($new_array);
print("</pre>");
//echo "<tr><td>" . $new_array[0] . "</td><td>" . $new_array[1] . "</td></tr>";// this doesnt work
}

Here's what the printed format spits out (abbreviated):

Array
(
[R] => "ABC company
)
Array
(
[R] => "ABC company
)
Array
(
[R] => "ABC company
)
Array
(
[R] => "ABC company
)
Array
(
[O] => XYZ company
)
Array
(
[O] => XYZ company
)
Array
(
[O] => XYZ company
)
Array
(
[O] => XYZ company
)

So - I'm getting the write letters assigned to write companys which is great. But my echo line:

echo "<tr><td>" . $new_array[0] . "</td><td>" . $new_array[1] . "</td></tr>";// this doesnt work

Do I have several arrays or a multidim array?

Thanks for any guidance

>>> "Richard Lynch" <ceo@l-i-e.com> 03/15/05 10:39AM >>>


> Hello
>
> I'm trying to manipulate a text(.csv) file using php. I've read the file
> and can print each line,
> but I need to write where column A is missing
>
> ColumnA ColumnB
>
> R ABC company
> ABC company
> ABC company
> ABC company
> O XYZ company
> XYZ company
> XYZ company
> XYZ company
>
> What I need to do is write a while loop (I think) to interate through the
> lines and populate ColumnA
> while ColumnB equals $var (ABC company, XYZ company), so that I end up
> with:
>
> ColumnA ColumnB
>
> R ABC company
> R ABC company
> R ABC company
> R ABC company
> O XYZ company
> O XYZ company
> O XYZ company
> O XYZ company
>
> Is this possible. What I've got so far to print out the file:
>
> $lines = file('test2.csv');
>


$category = '';

> // Loop through our array, and show line and numbers too.
> foreach ($lines as $line_num => $line) {
> echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";


//Assuming it's the FIRST character of each line that has space or category:
//Check first character:
if ($line[0] != ' ' && $line[0] != "\t"){
$category = $line[0];
}

echo "$category\t", trim(substr($line, 1)), "<br />\n";

> }
>
>
> Thanks in advance
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Like Music?
http://l-i-e.com/artists.htm
Reply With Quote
  #2 (permalink)  
Old 03-17-2005
Jochem Maas
 
Posts: n/a
Default Re: [PHP] file/array manipulation

Mignon Hunter wrote:
> Ok I got this to work but I'm not sure how -- and am having trouble re-producing the data:
>
> My script I have so far is:
>
> $lines = file('test2.csv');
> foreach ($lines as $line) {
> $line_split = explode(",", $line);
> if(!(empty($line_split[0]) ) ){
> //echo "I'm not empty";
> $new_array = array(
> $line_split[0] => $line_split[1]
> );
> }
> print("<pre>");
> print_r($new_array);
> print("</pre>");
> //echo "<tr><td>" . $new_array[0] . "</td><td>" . $new_array[1] . "</td></tr>";// this doesnt work
> }
>
> Here's what the printed format spits out (abbreviated):
>
> Array
> (
> [R] => "ABC company
> )
> Array
> (
> [R] => "ABC company
> )
> Array
> (
> [R] => "ABC company
> )
> Array
> (
> [R] => "ABC company
> )
> Array
> (
> [O] => XYZ company
> )
> Array
> (
> [O] => XYZ company
> )
> Array
> (
> [O] => XYZ company
> )
> Array
> (
> [O] => XYZ company
> )
>
> So - I'm getting the write letters assigned to write companys which is great. But my echo line:
>
> echo "<tr><td>" . $new_array[0] . "</td><td>" . $new_array[1] . "</td></tr>";// this doesnt work


given the array you printed above:

<?php

foreach ($new_array as $k => $v) {
echo "<tr><td>{$k}</td><td>{$v}</td></tr>";
}

>
> Do I have several arrays or a multidim array?


you create a single array for each line of the csv file. each array
contains one item. what you have done is assign the first field's value as the key of the
array item, and the second field's value as the array items value.

$t = array( "R" => "ABC company"); // array with 1 item, using associative key
var_dump($t[ "R" ],$t[ 0 ]);

$t = array( "R" , "ABC company"); // array with 2 items, using implicit numeric keys
var_dump($t[ "R" ],$t[ 0 ]);

>
> Thanks for any guidance
>
>
>>>>"Richard Lynch" <ceo@l-i-e.com> 03/15/05 10:39AM >>>

>
>
>>Hello
>>
>>I'm trying to manipulate a text(.csv) file using php. I've read the file
>>and can print each line,
>> but I need to write where column A is missing
>>
>>ColumnA ColumnB
>>
>>R ABC company
>> ABC company
>> ABC company
>> ABC company
>>O XYZ company
>> XYZ company
>> XYZ company
>> XYZ company
>>
>>What I need to do is write a while loop (I think) to interate through the
>>lines and populate ColumnA
>>while ColumnB equals $var (ABC company, XYZ company), so that I end up
>>with:
>>
>>ColumnA ColumnB
>>
>>R ABC company
>>R ABC company
>>R ABC company
>>R ABC company
>>O XYZ company
>>O XYZ company
>>O XYZ company
>>O XYZ company
>>
>>Is this possible. What I've got so far to print out the file:
>>
>>$lines = file('test2.csv');
>>

>
>
> $category = '';
>
>
>>// Loop through our array, and show line and numbers too.
>>foreach ($lines as $line_num => $line) {
>> echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";

>
>
> //Assuming it's the FIRST character of each line that has space or category:
> //Check first character:
> if ($line[0] != ' ' && $line[0] != "\t"){
> $category = $line[0];
> }
>
> echo "$category\t", trim(substr($line, 1)), "<br />\n";
>
>
>>}
>>
>>
>>Thanks in advance
>>
>>--
>>PHP General Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

>
>
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:01 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0