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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:02:12+00:00 2026-06-01T07:02:12+00:00

I got a result set in array format and i want to make square

  • 0

I got a result set in array format and i want to make square bracket value as a key and the other one as a value

here is my code

[Name]asdasdasd [Email]dfgdfg@in.com [Phone Number]42342342342 [Subject]dsafsdfsd [Company Name]ZXZXZX [Country]Antarctica 

i want output like :- array("name"=>"asdasdasd", "Email"=>"dfgdfg@in.com");

how can i do this in PHP any help would be greatly appreciated

Thanks Jassi

  • 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-01T07:02:14+00:00Added an answer on June 1, 2026 at 7:02 am

    You could do:

    <?php
    $str = '[Name]asdasdasd [Email]dfgdfg@in.com [Phone Number]42342342342 [Subject]dsafsdfsd [Company Name]ZXZXZX [Country]Antarctica ';
    
    preg_match_all('#\[([^\]]+)\]\s*([^\]\[]*[^\]\[\s])#msi', $str, $matches);
    
    $keys = $matches[1];
    $values = $matches[2];
    
    // PHP 5
    var_dump( array_combine($keys, $values) );
    ?>
    
    array(6) {
      ["Name"]=>
      string(9) "asdasdasd"
      ["Email"]=>
      string(13) "dfgdfg@in.com"
      ["Phone Number"]=>
      string(11) "42342342342"
      ["Subject"]=>
      string(9) "dsafsdfsd"
      ["Company Name"]=>
      string(6) "ZXZXZX"
      ["Country"]=>
      string(10) "Antarctica"
    }
    

    The regex is a bit more complicated looking, but it basically matches anything but [], allows whitespace in the value and makes sure that the last character isn’t [] or whitespace. You could probably get away with ([^\]\[\s]+) if you knew you were never going to have spaces.

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

Sidebar

Related Questions

I've got a query that returns a proper result set, using SQL 2005. It
(NOTE: Source code here https://github.com/cthielen/dss-evote ) I've got a simple voting application. A survey
I've got a problem with the mysqli result set. I have a table that
I'm trying to complete this assignment, I've got the code set up, however, there's
I've got some code, and I've been trying to make some minor tweaks to
I've got the following array $weight and I want to store these values to
I've got a set of tables named results_%, all with the same structure. I
I fired a database query and got result in a DataTable . When I
i got 3 linked list and union function a b and result are lists
i've got an integration test that grabs some json result from a 3rd party

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.