0

I have a while loop that loops through line of text

while ($line_of_text = fgetcsv($file_processing, 4096)) {

In this while loop I assign variables to different parts of the array

   IF($i > 0)
   {
    echo "</br>";
    $account_type_id = $line_of_text[0];
    echo "Account Type ID: " . $account_type_id. "<br>";

    $account_number = $line_of_text[1];
    echo "account_number = " . $account_number . "<br>";

This while loop loops through many lines. I am trying to find a way to say that

IF $account_type_id == 99 then add $account_number to an array. Then outside of the while loop print out the whole array of $account_numbers where $account_type_id == 99.

I have tried using print_r but it will only display the last array...

1
  • if($account_type_id == 99){ $account_numbers[] = $account_numbers; }? Commented Sep 8, 2015 at 18:36

2 Answers 2

1

To add the element to an array, you can use array_push.

First you need to create the array (before the while loop):

$my_array = array();

Then, in the while loop, do this:

if ($account_type_id == 99) {
    array_push($my_array, $account_number);
}

Then to display the array, either use print_ror var_dump. To make the array easier to read, you can also do this:

echo "<pre>".print_r($my_array, 1)."</pre>";
Sign up to request clarification or add additional context in comments.

4 Comments

$my_array = new array(); is not PHP syntax.
Simple mistake to change. Don't see the need to downvote, but hey ho!
you can refer to my first comment.
But the rest was correct. As I said, a simple mistake was made and it was now been rectified. My last comment was just to check I had not made any other mistakes ;)
0

Rocket H had the answer in the comment he posted

inside of your loop

if($account_type_id == 99){
    $account_numbers[] = $account_number;
   }

After loop

print_r($account_numbers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.