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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:33:19+00:00 2026-05-24T04:33:19+00:00

Sometime back I was getting alot of data via some API and I saved

  • 0

Sometime back I was getting alot of data via some API and I saved it into a flat file doing a simple var_dump or print_r. Now I am looking to process the data and each line looks like:

” ‘middle_initial’ => ”, ‘sid’ => ‘1419843’, ‘fixed’ => ‘Y’,
‘cart_weight’ => ‘0’, ‘key’ => ‘ABCD’, ‘state’ => ‘XX’, ‘last_name’
=> ‘MNOP’, ’email’ => ‘abc@example.com’, ‘city’ => ‘London’,
‘street_address’ => ‘Sample’, ‘first_name’ => ‘Sparsh’,”

Now I need to get this data back into an array format. Is there a way I can do that?

  • 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-24T04:33:20+00:00Added an answer on May 24, 2026 at 4:33 am

    What about first exploding the string with the explode() function, using ', ' as a separator :

    $str = "'middle_initial' => '', 'sid' => '1419843', 'fixed' => 'Y', 'cart_weight' => '0', 'key' => 'ABCD', 'state' => 'XX', 'last_name' => 'MNOP', 'email' => 'abc@example.com', 'city' => 'London', 'street_address' => 'Sample', 'first_name' => 'Sparsh',";
    $items = explode(', ', $str);
    var_dump($items);
    

    Which would get you an array looking like this :

    array
      0 => string ''middle_initial' => ''' (length=22)
      1 => string ''sid' => '1419843'' (length=18)
      2 => string ''fixed' => 'Y'' (length=14)
      3 => string ''cart_weight' => '0'' (length=20)
      ...
    

    And, then, iterate over that list, matching for each item each side of the =>, and using the first side of => as the key of your resulting data, and the second as the value :

    $result = array();
    foreach ($items as $item) {
        if (preg_match("/'(.*?)' => '(.*?)'/", $item, $matches)) {
            $result[ $matches[1] ] = $matches[2];
        }
    }
    var_dump($result);
    

    Which would get you :

    array
      'middle_initial' => string '' (length=0)
      'sid' => string '1419843' (length=7)
      'fixed' => string 'Y' (length=1)
      'cart_weight' => string '0' (length=1)
      ...
    

    But, seriously, you should not store data in such an awful format : print_r() is made to display data, for debugging purposes — not to store it an re-load it later !

    If you want to store data to a text file, use serialize() or json_encode(), which can both be restored using unserialize() or json_decode(), respectively.

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

Sidebar

Related Questions

I am working on an application in which getting data back to a serial
Some time back, I created two separate SharePoint projects in VS 2008; one using
I ran across a small framework some time back that someone put together for
We built an app for a client some time back, signed it with our
This question has in the back of my mind for some time, sorry if
I have been spending some time on this. My problem involves bringing back domain
I'm facing an issue with merging a branch back into a tree on SVN,
I found out a method which is changed some time back and I need
we're getting this exception sometimes when trying to write a file to the Blobstore.
I'm slowing getting to know Vim and Bash shell scripting and am running into

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.