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

  • Home
  • SEARCH
  • 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 7858675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:33:20+00:00 2026-06-02T21:33:20+00:00

I am learning how to use PHP. I read a file content into an

  • 0

I am learning how to use PHP. I read a file content into an array and assign variable name for each index in the array.

For example:

$words = file("example.txt"); #each line of the file will have the format a, b, c , d

foreach ($words in $word) {  
$content = explode(",", $word); #split a, b, c, d  
list($a, $b, $c, $d) = $content;  
do something  
}  

/* And now I want to read file, split the sentence and loop over the array again, but
 the last statement will do something else different:   */
foreach ($words in $word) {  
$content = explode(",", $word); #split a, b, c, d  
list($a, $b, $c, $d) = $content;  
do something else different  
} 


What can I do to reduce this redundancy? As you can see, I cannot make a function because the last statement do something different to the array. But the process of reading file, splitting sentences, and assigning vars are the same

Thank you

  • 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-02T21:33:28+00:00Added an answer on June 2, 2026 at 9:33 pm

    I’m assuming you meant to type foreach($words as $word), with “as” instead of “in”, but that’s just a minor thing compared to the question.

    You can certainly reduce the redundancy by storing the results of the explode calls:

    $lines = Array();
    foreach($words as $word) {
        list($a,$b,$c,$d) = $lines[] = explode(",",$word);
        // do something here
    }
    
    foreach($lines as $line) {
        list($a,$b,$c,$d) = $line;
        // do something else
    }
    

    This way you don’t have to explode the line again.

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

Sidebar

Related Questions

I am learning php, trying to use the fopen() function. The php file I
When I was learning PHP, I read somewhere that you should always use the
I'm learning to use the PHP interactive shell, but I'm having trouble with multi-line
I am learning to use json_decode . I want to try out some php
I am learning how to use AJAX and PHP json_encode() . I am returning
So my entire site runs off of an index.php page and the content loads
I'm working on learning to use prepared statements with mysqli in PHP and usually,
Is there any way to use a php file in the parent folder. I
I'm learning to use PHP, CSS and XHTML. I would like to do a
I'm learning to use vlad the deployer and I've got a question. Since I'm

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.