Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:02:19+00:00 2026-06-04T01:02:19+00:00

Using PHP to try and tackle this problem, but open to other solutions (Python,

  • 0

Using PHP to try and tackle this problem, but open to other solutions (Python, Bash, etc).

I have csv data in the following format:
Note: store name id (rows 1 and 6) is always 10 digits
Product id (col1, rows 2,3,4 and 7,8,9,10,11) is always 7 digits.

Store name 0123456789,,,
0123456,product desc,1,1.00
1234567,product desc2,1,2.00
2345678,product desc3,1,3.00
Ship Total,6.00,,
Store name2 9876543210,,,
0123456,product desc,4,1.00
1234567,product desc2,2,2.00
2345678,product desc3,1,3.00
3456789,product desc4,3,4.00
45678901,product desc5,1,5.00
Ship Total,28.00,,

The desired format is:

0123456789,0123456,product desc,1,1.00
0123456789,1234567,product desc2,1,2.00
0123456789,2345678,product desc3,1,3.00
9876543210,0123456,product desc,4,1.00
9876543210,1234567,product desc2,2,2.00
9876543210,2345678,product desc3,1,3.00
9876543210,3456789,product desc4,3,4.00
9876543210,45678901,product desc5,1,5.00

I have a program to parse the data in the above format.

I’ve gotten the stores into an array and the transactions into another array… just need to put them together.

Here’s what I’ve got so far.

$csv = array();
$file = fopen($datafile, 'r');

while (($result = fgetcsv($file)) !== false)
{
    $csv[] = $result;
}

fclose($file);

foreach ($csv as $key => $value) { 
    if (preg_match('/\d{10}$/', $value[0],$store)) {
        $stores[$key] .= $store[0];
    }
}
print_r($stores);

foreach ($csv as $key => $value) {
    if (preg_match('/^\d{7}/', $value[0],$transaction)) {
        $transactions[$key] = array("Item"=>$value[0],"Desc"=>$value[1],"Qty"=>$value[2],"Price"=>$value[3]);
    }
}
print_r($transactions)

print_r results:

Array
(
    [0] => 0123456789
    [5] => 9876543210
)

Array
(
    [1] => Array
        (
            [Item] => 0123456
            [Desc] => product desc
            [Qty] => 1
            [Price] => 1.00
        )
...
... arrays 2,3,4,6,7,8,9....
...

    [10] => Array
        (
            [Item] => 45678901
            [Desc] => product desc5
            [Qty] => 1
            [Price] => 5.00
        )
)

EDIT after answers. Here's what worked perfectly.

[code]

$file = fopen($datafile, 'r'); 

$txns = array();
$LastStore = '';
while (($result = fgetcsv($file)) !== false) 
{ 
    if (preg_match('/\d{10}$/', $result[0],$store)) { 
        $LastStore = $store;
    } elseif (preg_match('/[A-Za-z]+/', $result[0])) {
        continue;
    } else {
        $txns[] = array("Store"=>$LastStore[0], "Item"=>$result[0],"Desc"=>$result[1],"Qty"=>$result[2],"Price"=>$result[3]);
    }
} 
fclose($file);

[/code]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:02:21+00:00Added an answer on June 4, 2026 at 1:02 am

    I would suggest something like this

    $handle = @fopen($datafile, "r");
    if ($handle) {
        $Out = '';
        $Store = '';
        while (($buffer = fgets($handle)) !== false) {
            if (substr($buffer, 0, 5) == 'Store') {
                preg_match('/\d{10}/', $buffer, $storeId);
                $Store = $storeId[0] . ',';
            } else if (substr($buffer, 0, 4) == 'Ship') {
                // ignore
            } else {
                $Out .= $Store . $buffer . "\n";
            }
        }
    }
    fclose($handle);
    
    file_put_contents('Results.txt', $Out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to program a webboot using PHP/CURL, but I face a problem in
I'm creating a login for a website using PHP but when I try to
I am try to install amqp for php (Integrating PHP with RabbitMQ) using this
Im using Debian testing, i have installed php-cli and when i try to execute
I'm using NetBeans and PHP. For example, I insert this code: <?php try {
Right, I'll try and explain this the best I can. I'm using PHP COM
Using this php code: try{ $dbh = new PDO(mysql:host=$host;dbname=$dbname,$user,$pass); $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //
I'm trying to write someting into the DB using PHP but if I try
I have the following code on my site (using php and smarty) to try
When I try to reload my fixtures using php app/console doctrine:fixtures:load I'm getting this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.