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 8222537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:18:54+00:00 2026-06-07T14:18:54+00:00

Consider the following data.csv: 1, 2, 3, 4 5, 6, 7, 8 9,10,11,12 13,14,15,16

  • 0

Consider the following data.csv:

 "1", "2", "3", "4"
 "5", "6", "7", "8"
 "9","10","11","12"
"13","14","15","16"
"17","18","19","20"
"21","22","23","24"
"25","26","27","28"
"29","30","31","32"
"33","34","35","36"

In reality, the rows and columns are much longer, but the principle stays the same.

I need a way to read the csv file, remove the quotes and concatenate every 3 consecutive rows to each other to format the following output:

1,2,3,4,5,6,7,8,9,10,11,12
13,14,15,16,17,18,19,20,21,22,23,24
25,26,27,28,29,30,31,32,33,34,35,36

I now have:

$path = "data.csv";
$row = 0;
$newrow = 0;
$newrows = array();
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $newrows[$newrow] = implode("," $data);
    if ($row % 3) $newrow++;
    $row++;
  }
  fclose($handle);
}

What I’m trying to do is create an array “newrows” (see below) in which data is added to the current new row while $row cannot be devided by 3

$newrows = array (
  [0] = "1,2,3,4,5,6,7,8,9,10,11,12",
  [1] = "13,14,15,16,17,18,19,20,21,22,23,24",
  [2] = "25,26,27,28,29,30,31,32,33,34,35,36"
)

My code obviously isn’t working, but I am confused as how to proceed. Do you know? Any help is greatly appreciated 🙂

edit I seem to have made a mistake. The output should not be “concatenate every set of 3 rows” but rather “concatenate every third row”, so:

  • every 3rd row is concatenated to the previous 3rd one
  • row 4 (1 + 3) and 7 (1 + 3 + 3) are concatenated to row 1
  • row 5 (2 + 3) and 8 (2 + 3 + 3) are concatenated to row 2
  • row 6 (3 + 3) and 9 (3 + 3 + 3) are concatenated to row 3

The output then would be an array:

array (
  [0] => 1,2,3,4,13,14,15,16,25,26,27,28
  [1] => 5,6,7,8,17,18,19,20,29,30,31,32
  [2] => 9,10,11,12,21,22,23,24,33,34,35,36
)

I tried this but it concatenates incorrect:

$path = "data.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    for ($i = 1; $i <= 3; $i++) {
      if ($row % $i == 0) $newrows[$i] .= implode(",", $data);
    }
    $row++;
  }
}

print_r($newrows);

Array (
  [1] => 1,2,3,45,6,7,89,10,11,1213,14,15,1617,18,19,2021,22,23,2425,26,27,2829,30,31,3233,34,35,36
  [2] => 5,6,7,813,14,15,1621,22,23,2429,30,31,32
  [3] => 9,10,11,1221,22,23,2433,34,35,36
)

P.S. In reality, the csv is much larger and I need every 147th row to be concatenated to the previous 147th row, but the principle is the same I guess.

  • 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-07T14:18:55+00:00Added an answer on June 7, 2026 at 2:18 pm

    Your while loop needs some work:

    $newrow = 0; $row = 1;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if( !isset( $newrows[$newrow])) $newrows[$newrow] = '';
        $newrows[$newrow] .= implode(",", $data);
        if ($row % 3 == 0) {
            $newrow++;
        } else {
            $newrows[$newrow] .= ', ';
        }
        $row++;
    }
    

    Notable changes:

    • $row now starts at 1, so the first three iterations will be mapped to the same entry.
    • $newrows[$newrow] = ''; is properly initialized.
    • $row % 3 is compared to == 0, which determines if we’re at the end of every third row correctly.
    • $newrows[$newrow] is set to .= implode(",", $data);, which will continually concatenate rows together. Otherwise, only the last iteration would be kept in your original code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a CSV file with the following data: weight,lat,long 9876,23.44,88.15 1234,27.11,55.33 .... What is
Consider the following data structure: List<Person> People; class Person { List<Car> Cars; List<Hobby> Hobbies;
For my question lets consider the following sample table data: ProductID    ProductName    Price   Category 1                Apple                 5.00       Fruits
Consider this scenario: I've an XML file called person.xml with the following data in
Consider the following two FFI structs: class A < FFI::Struct layout :data, :int end
Consider the following header file: template <typename T> struct tNode { T Data; //the
Consider the following: package MyApp::CGI; use Moose; use MooseX::NonMoose; use Data::Dumper; extends 'CGI::Application'; BEGIN
Consider the following data in long format library(plyr) library(reshape2) x <- seq(0,2*pi,length=20) ll <-
Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Consider the following two scenarios: //Data Contract public class MyValue { } Scenario 1:

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.