homepage Welcome to WebmasterWorld Guest from 204.236.254.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Splitting a array
Matthew1980




msg:4407454
 9:07 pm on Jan 16, 2012 (gmt 0)

Hi there people,

I'm going mad as this to me would seem easy to do, but I cannot fathom this out today:-

I read a file:-

FIRST_LINE, a value
SECOND_LINE, another value
THIRD_LINE, yet another value

Then that geat read into an array:-

FirstArray[0] = "FIRST_LINE, a value"
FirstArray[1] = "SECOND_LINE, another value"
FirstArray[1] = "THIRD_LINE, yet another value"

What I would like to know is, I would really prefer to have this data in this format:-

FirstArray[0] = "FIRST_LINE"
SecondArray[0] = " a value"
FirstArray[1] = "SECOND_LINE"
SecondArray[1] = "another value"
FirstArray[2] = "THIRD_LINE"
SecondArray[2] = "yet another value"

I can't see how to achieve this. Anyone would like to tell me why I can't see the wood for the trees please?

Cheers,
MRb

 

omoutop




msg:4407575
 7:15 am on Jan 17, 2012 (gmt 0)

if all your data is in that particular format [val1, val2]
then try to explode each line.

foreach ($line as $data)
{
$temp = explode(",", $data);
$array1[] = $temp[0];
$array2[] = $temp[1];
}

note: this method will fail if either [FIRST_LINE] or [value]
contain (,) characters

Matthew1980




msg:4407577
 7:34 am on Jan 17, 2012 (gmt 0)

Cheers omoutop,

I can't fathom out why I could't work this simple little snippet out! I guess it's because it was Monday and I'm tired ;)

That'll do nicely. I'll just tweek it so that I can do it dynamically, but that's cool.

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved