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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:38:55+00:00 2026-05-20T13:38:55+00:00

I have a .txt document set up like this: HAYLE08 VALUE X VALUE Y

  • 0

I have a .txt document set up like this:

HAYLE08
VALUE X
VALUE Y

BRUNO10
VALUE X
VALUE Y

Which needs to processed to a multidimensional array like this:

Array
(
    [HAYLE08] => Array
        (
            [0] => Value X
            [1] => Value Y
        )

    [BRUNO10] => Array
        (
            [0] => Value X
            [1] => Value Y
        )

)

Reading the file into php is no problem and exploding the different chunks of 3-lines is very easy like this:

$file = file_get_contents('test.txt'); 
$lines = explode( "\n\n", $file );

But of course, this will only give me the first step:

Array
(
    [0] => HAYLE08
VALUE X
VALUE Y
    [1] => BRUNO10
VALUE X
VALUE Y

)

I’ve tried different foreaches and other loops or line explodes to populate the other dimensions, yet all in vain. I feel kind of stupid to ask such a simple question, but even after researching I just seem to be missing some basic array-logic here.
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-05-20T13:38:56+00:00Added an answer on May 20, 2026 at 1:38 pm

    Your almost there. You do want to use another loop / explode. Something like this:

    $lines = explode( "\n\n", $file );
    $return = array();
    
    foreach ($lines as $line) {
      $items = explode("\n", $line);
      $return[array_shift($items)] = $items;
    }
    
    print_r($return);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to read a txt file with lines formated like this: 1: (G,
I have a java.hprof.txt file (automatically generated after an OutOfMemoryError) which I would like
I have a VBScript script that looks like this: Const ForAppending = 8 Set
I have a txt file which actually is a html source of some webpage.
I have a generated txt file. This file has certain lines that are superfluous,
I have a file 'records.txt' which contains over 200,000 records. Each record is on
I have been using this code to read in a file from a document
I have an XML document file.xml which is encoded in Iso-latin-15 (aka Iso-Latin-9) <?xml
I have set a crawler up in Wordpress which grabs stocks data and writes
I have a .txt file that I need to include into a page, that

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.