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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:54:01+00:00 2026-06-14T23:54:01+00:00

I feel like I’ve done something very silly, but I can’t work out what

  • 0

I feel like I’ve done something very silly, but I can’t work out what it is.
I’m running through an array of arrays generated by fgetcsv(). What I want is to split the array in two at the spot where the value in one line differs from the one in the next.
This code results in $first holding all but one of the arrays, and $second holding the last – completely ignoring the string comparison. strcmp() results in the same thing.

$csv = array();
$file = fopen('A.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
    $csv[] = $result;
}
fclose($file);

array_shift($csv); //gets rid of the CSV headers
$rows = count($csv);

for ($i = 0; $i <= $rows; $i++){
    if ($csv[$i][1] != $csv[$i+1][1]){
        $first = array_slice($csv, 0, $i);
        $second = array_slice($csv, $i);
    }
}

Here’s an example of the CSV file:

NAME,MATCHNAME,CHROMOSOME,START LOCATION,END LOCATION,CENTIMORGANS,MATCHING SNPS
A,person_one,2,20945970,23287731,2.48,500
A,person_one,2,233444593,234432885,1.56,500
A,person_one,4,99184637,100861136,1.24,500
A,person_two,1,154990798,157871980,2.8,700 //Here's where the new array should start
A,person_two,1,67136078,70785393,2.28,800

EDIT: My expected $first for this example would be:

Array
(
[0] => Array
    (
        [0] => A
        [1] => person_one
        [2] => 2
        [3] => 20945970
        [4] => 23287731
        [5] => 2.48
        [6] => 500
    )

[1] => Array
    (
        [0] => A
        [1] => person_one
        [2] => 2
        [3] => 233444593
        [4] => 234432885
        [5] => 1.56
        [6] => 500
    )

[2] => Array
    (
        [0] => A
        [1] => person_one
        [2] => 4
        [3] => 99184637
        [4] => 100861136
        [5] => 1.24
        [6] => 500
    )
)

And my expected $second would be:

Array
(
[0] => Array
    (
        [0] => A
        [1] => person_two
        [2] => 1
        [3] => 154990798
        [4] => 157871980
        [5] => 2.8
        [6] => 700
    )

[1] => Array
    (
        [0] => A
        [1] => person_two
        [2] => 1
        [3] => 67136078
        [4] => 70785393
        [5] => 2.28
        [6] => 800
    )
)
  • 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-14T23:54:02+00:00Added an answer on June 14, 2026 at 11:54 pm

    It is doing what you want, but when it gets to the last iteration, it compares whatever’s in the last line with null, so it overwrites $first and $second.

    Try adding break; after the assignments to break out of the loop when the condition is met.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I'm missing something obvious, but I can't work out why my
I feel like I'm missing something very obvious here, but I can't get a
I feel like I am missing something very simple here, but this is the
I feel like this should be simple, but can't figure it out: What's the
I feel like this is a very noob question.. but I just can't get
I feel like there's something about grids that I'm missing, but I can't find
I feel like this must be simple to do but can't get it done
I feel like I've only ever seen this here on SO, but I can't
I feel like this is something I should already know, but I'm just not
I feel like this must be very simple, but for some reason I am

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.