Jump to content

Building an Array from an Array, within a loop. Ideas?


monkeytooth

Recommended Posts

Ok, im stuck. I havent tempted to build it yet as I cant wrap my mind around it fully at the moment so I am looking for any feedback. I have several large text files that have a csv construct to them. Each text file is several thousand lines deep. First I need to build an array I assume breaking it down line by line. But then I need to take each line and break it further down. So each line becomes chunks of itself based on the csv values.

 

What I am doing is trying to put this information into a database table. Yes, I could use phpmyadmin but 1 its limited to the file size which these file smash that limit to an extreme. 2 phpmyadmin wont let me specify the delimiter for the csv (and if phpmyadmin does, it doesn't on the version my host provides).

 

Example of the layout I need to break down as mentioned above:

 

Raw Info:

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

 

I need to take that and find each line:

array[1] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

array[2] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

array[3] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

array[4] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

array[5] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

array[6] = abc|def|ghi|jkl|mno|pqr|stu|vwx|ya

 

then I need to make each line break apart into different vars:

array2[1] = abc

array2[2] = def

array2[3] = ghi

array2[4] = jkl

array2[5] = mno

array2[6] = pqr

array2[7] = stu

array2[8] = vwx

array2[9] = yz

 

from there I am just using popping the var's into an insert string and putting them into the DB.

 

My main issue is the thought process, I can't think of how I would break down the loops to do this. Without either causing an endless loop or breaking the loops cause I am building them wrong.

 

Any idea's for this? I know I can use explode to break them down, and filter them individually but how to get them to work with one another sync'd doing both at once is beyond my thought process at the moment. I just need help getting the concept rolling. So if you got an idea please throw it at me.

 

Link to comment
Share on other sites

What does the data represent? Will each item be a separate record in the database or what? Here's an example of doing what you stated:

 

<?php
$text = <<<TEXT
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
abc|def|ghi|jkl|mno|pqr|stu|vwx|ya
TEXT;

//$file = file('...');
$file = explode("\n", $text);
$arr2 = Array();
foreach($file as $line)
{
$arr[] = $line;
$arr2 = array_merge($arr2, explode('|', $line));
}
print_r($arr);
print_r($arr2);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.