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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:56:16+00:00 2026-05-23T01:56:16+00:00

I am experimenting with loading CSV in PHP. I have a CSV file, with

  • 0

I am experimenting with loading CSV in PHP.

I have a CSV file, with 3 columns, simple values.

I have managed to be able to loop through each line, and output each value, but I need to grab data conditionally based on unique values and based on a certain position in the row.

For example, for each unique value in column a, I need to generate an array of data objects based on the value of column b, using column c as a conditional for one of the values.

My CSV:

1   123 0
1   124 0
1   125 0
1   126 0
1   127 0
1   128 0
1   129 0
1   130 1
1   131 1
1   132 1
1   133 1
1   134 1
1   135 1
2   123 0
2   124 0
2   125 0
2   126 1
2   127 1
2   128 1
2   129 1
2   130 1
2   131 1
2   132 1
2   133 1
2   134 1
2   135 1
3   256 0
3   456 0
3   321 0
3   489 0
3   965 0
3   652 1
3   741 1

Code I am using to interrogate:

<?php
$stack = array();
if (($han = fopen("sample.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
        array_push($stack, $data); //push each arr1 into arr2(stack)
    }
    fclose($han);
}
sort($stack); //sort arr2
$newarray = array();
foreach($stack as $val){
$lineid = $val[0];
$segmentid = $val[1];
$action = $val[2];
$newarray[$lineid][$segmentid] = $action;
}
print_r($newarray);
?>

Output

Array
(
    [1] => Array
        (
            [123] => 0
            [124] => 0
            [125] => 0
            [126] => 0
            [127] => 0
            [128] => 0
            [129] => 0
            [130] => 1
            [131] => 1
            [132] => 1
            [133] => 1
            [134] => 1
            [135] => 1
        )

    [2] => Array
        (
            [123] => 0
            [124] => 0
            [125] => 0
            [126] => 1
            [127] => 1
            [128] => 1
            [129] => 1
            [130] => 1
            [131] => 1
            [132] => 1
            [133] => 1
            [134] => 1
            [135] => 1
        )

    [3] => Array
        (
            [256] => 0
            [321] => 0
            [456] => 0
            [489] => 0
            [652] => 1
            [741] => 1
            [965] => 0
        )

)

psuedocode for what I am wanting to achieve

for each unique column_a value {
grab all column_b's within this unique column_a and for each
    $column_b_object = new stdClass;
    $column_b_object->id = $column_b_value;
      if (column_c_value = "0") {
        $column_b_object->zero = "no";
      }
    $column_bs[] = $column_b_object;
    execute something, reset, move to next unique column_a
}

EDIT SOLUTION:

<?php
$stack = array();
if (($han = fopen("sample.csv", "r")) !== FALSE) {
while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
array_push($stack, $data); //push each arr1 into arr2(stack)
}
fclose($han);
}
sort($stack); //sort arr2
$newarray = array();
foreach($stack as $val){
$lineid = $val[0]; $segmentid = $val[1]; $action = $val[2];
$newarray[$lineid][$segmentid] = $action;
}
foreach($newarray as $value) {
foreach($value as $key => $value2){
echo $key . " | " . $value2 . "<br />";
}
echo "STOP";
}
?>

Hope that makes sense.

Thanks

  • 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-05-23T01:56:17+00:00Added an answer on May 23, 2026 at 1:56 am

    There is something wrong with your loop logic. If you still gonna iterate through every single “b” element why do you have to define the uniqueness of your “a” element? Anyway, maybe it is just me, but if you still want to do it the way your psuedo is you can:
    1. Convert line from your csv into array1
    2. Store that array1 into another array2
    3. Sort arraty2
    4. Loop through array2 and keep track of any changes in the first element (element a)

    $i=0;
    $stack = array();
    if (($han = fopen("3col.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($han, 50, ",")) !== FALSE) {
            array_push($stack, $data); //push each arr1 into arr2(stack)
            $i++;
            $num = count($data);
            echo "<p>";
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "|";
            }
            echo "</p>";
        }
        fclose($han);
    }
    
    sort($stack); //sort arr2
    
    foreach($stack as $key => $arr2){
        $sameA = ($arr2[0] == $a) ? "" : "-->";
            echo $sameA;
    
        foreach($arr2 as $key => $val){ 
            echo $val." ";
        }
            $a = $arr2[0];
            echo "<br/>";
    }
    

    and the Output:

    -->1 | 123 | 0 | 
    1 | 124 | 0 | 
    1 | 125 | 0 | 
    1 | 126 | 0 | 
    1 | 127 | 0 | 
    1 | 128 | 0 | 
    1 | 129 | 0 | 
    1 | 130 | 1 | 
    1 | 131 | 1 | 
    1 | 132 | 1 | 
    1 | 133 | 1 | 
    1 | 134 | 1 | 
    1 | 135 | 1 | 
    -->2 | 123 | 0 | 
    2 | 124 | 0 | 
    2 | 125 | 0 | 
    2 | 126 | 1 | 
    2 | 127 | 1 | 
    2 | 128 | 1 | 
    2 | 129 | 1 | 
    2 | 130 | 1 | 
    2 | 131 | 1 | 
    2 | 132 | 1 | 
    2 | 133 | 1 | 
    2 | 134 | 1 | 
    2 | 135 | 1 | 
    -->3 | 256 | 0 | 
    3 | 321 | 0 | 
    3 | 456 | 0 | 
    3 | 489 | 0 | 
    3 | 652 | 1 | 
    3 | 741 | 1 | 
    3 | 965 | 0 | 
    

    You can see an arrow every time you start new “a” element

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

Sidebar

Related Questions

I am experimenting with JavaScript & I can't seem to understand simple behavior. What
I'm just starting experimenting with jQuery and trying to apply it to a very
Experimenting with a battery monitor icon at the moment in Python using pygtk and
I'm experimenting with letting the iPhone user draw using the canvas and then having
I am experimenting with the Java UIManager and the use of different Look and
I'm experiencing a similar problem to this guy except my problem is related to
I am working on a site right now that is experiencing a slow down
A stylesheet being loaded inside an IE conditional tag is being loaded in Google
I'm making an Android app where the user can download files from a FTP-server.
I am experiencing a problem with Swing that only occurs when the computer monitor

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.