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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:41:46+00:00 2026-06-09T21:41:46+00:00

I have some logic that builds a multi-dimensional array based on matches found in

  • 0

I have some logic that builds a multi-dimensional array based on matches found in a regex. I call the explode function,using a delimiter.
Everything works and my array looks like this:

 Array ( 
  [0] => 
    Array ( 
        [0] => A1 
        [1] => 100/1000T 
        [2] => No 
        [3] => Yes 
        [4] => Down 
        [5] => 1000FDx 
        [6] => Auto 
        [7] => off 
        [8] => 0 
    ) 
[1] => Array ( 
        [0] => A2 
        [1] => 100/1000T 
        [2] => No 
        [3] => Yes 
        [4] => Down 
        [5] => 1000FDx 
        [6] => Auto 
        [7] => off 
        [8] => 0 
      ) etc.etc...

In order to keep the code in the front end “dumb”, i want to change the keys from numbers to strings that represent what the values are. These strings will be used as column headings in a table. So for example:

 Array ( 
  [0] => 
    Array ( 
        [port] => A1 
        [Type] => 100/1000T 
        [Alert] => No 
        [Enabled] => Yes 
        [Status] => Down 
        [Mode] => 1000FDx 
        [MDIMode] => Auto 
        [FlowCtrl] => off 
        [BcastLimit] => 0 
    ) 
[1] => Array ( 
        [port] => A2 
        [Type] => 100/1000T 
        [Alert] => No 
        [Enabled] => Yes 
        [Status] => Down 
        [Mode] => 1000FDx 
        [MDIMode] => Auto 
        [FlowCtrl] => off 
        [BcastLimit] => 0         
               ) etc.etc...

Here’s the code that generates this array:

  $portdetailsArray = array();
  foreach ($data as $portdetails) {
    $pattern = '/(\s+)([0-9a-z]*)(\s+)(100\/1000T|10|\s+)(\s*)(\|)(\s+)(\w+)(\s+)(\w+)(\s+)(\w+)(\s+)(1000FDx|\s+)(\s*)(\w+)(\s*)(\w+|\s+)(\s*)(0)/i';

   if (preg_match($pattern, $portdetails, $matches)) {

        $replacement = '$2~$4~$8~$10~$12~$14~$16~$18~$20';
        $portdetails= preg_replace($pattern, $replacement, $portdetails);
        array_push($portdetailsArray, explode('~',$portdetails));
   }

}

I guess instead of using the explode function, I can manually loop through my string. Each time I find a “~”, i know it’s the start of a new field so i can add they key /value pair manually.
But I was just wondering if anyone had ideas on other ways to do this.
Thanks.

  • 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-09T21:41:48+00:00Added an answer on June 9, 2026 at 9:41 pm

    To reply to your original question, you could use the array_combine function, to replace the keys.

    $row = explode('~',$portdetails);
    $row = array_combine(array(
           'port',
           'Type',
           'Alert',
           'Enabled',
           'Status',
           'Mode',
           'MDIMode',
           'FlowCtrl',
           'BcastLimit'), $row);
    

    But even better, you should use the clearer (verbose is clearer in this case)

    if (preg_match($pattern, $portdetails, $matches)) {
        array_push($portdetailsArray, array(
           'port' => $matches[2],
           'Type' => $matches[4],
           'Alert' => $matches[8],
           'Enabled' => $matches[10],
           'Status' => $matches[12],
           'Mode' => $matches[14],
           'MDIMode' => $matches[16],
           'FlowCtrl' => $matches[18],
           'BcastLimit' => $matches[20]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some logic that depends upon two properties being set, as it executes
I have some custom logic that needs to be executed every single time a
In XCode 4.2, I have some logic tests that I want to exercise against
I have some logic in a method that operates on a specified type and
So here is the problem: I have some logic in my application that will
I have a library that I created with some business logic that includes writing
I am creating a series of UserControls that all have some similar business logic.
I have a HtmlHelper extension method that I would like to apply some logic
I have a service object that is responsible for some business logic validation. What
Let's say you have a business logic method that can perform some operation across

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.