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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:15:22+00:00 2026-05-26T06:15:22+00:00

i have a (dynamic) array, which in this example contains 4 sets of data

  • 0

i have a (dynamic) array, which in this example contains 4 sets of data (5 fields per set), but it could be only one set or up to 25 sets.

Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => ) 

which i’d like to turn into something like

Array ( Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => ),
        Array ( [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => ),
        Array ( [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => ), 
        Array ( [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
        )

the original array is created this way:

$light = Array();

foreach( $_POST as $key => $val )
{
//field names that start with light to go in this array
    if (strpos($key, 'light') === 0) {
        $light[$key] = $val;
    }
}

the field name digit is already added with JS before form submission, and not by php script.
any hint much appreciated.

  • 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-26T06:15:23+00:00Added an answer on May 26, 2026 at 6:15 am

    Try this:

    $prefixes = array();
    $postfixes = array();
    foreach($original_array as $key=>$value)
    {
            preg_match('/^([^\d]*)(\d+)$/',$key,$matches);
            if(count($matches)>1)
            {
                    if(!in_array($matches[1], $prefixes)) $prefixes[] = $matches[1];
                    if(!in_array($matches[2], $postfixes)) $postfixes[] = $matches[2];
            }
    }   
    $new_array = array();
    foreach($postfixes as $postfix)
    {
            $new_element = array();
            foreach($prefixes as $prefix)
            {
                    if(isset($original_array[$prefix.$postfix])) $new_element[$prefix.$postfix] = $original_array[$prefix.$postfix];
            }
            $new_array[] = $new_element; 
    }
    

    given an $original_array equal to described, will produce $new_array:

    Array
    (
        [0] => Array
            (
                [lightwattage1] => 50
                [lightvoltage1] => 12
                [lightquantity1] => 2
                [lightusage1] => 4
                [lightcomment1] => 
            )
    
        [1] => Array
            (
                [lightwattage2] => 60
                [lightvoltage2] => 24
                [lightquantity2] => 4
                [lightusage2] => 5
                [lightcomment2] => 
            )
    
        [2] => Array
            (
                [lightwattage3] => 30
                [lightvoltage3] => 240
                [lightquantity3] => 4
                [lightusage3] => 2
                [lightcomment3] => 
            )
    
        [3] => Array
            (
                [lightwattage4] => 25
                [lightvoltage4] => 12
                [lightquantity4] => 2
                [lightusage4] => 6
                [lightcomment4] => 
            )
    
    )
    

    I was uncertain about how much you knew about the elements or their order, so this code basically takes any collection of elements that end in numbers and rearranges them in groups that have the same ending number.

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

Sidebar

Related Questions

So I have this 2d dynamic array which content I want to free when
I have dynamic array filled with bytes, which are read from .raw file with
I have an array of colors having dynamic values which depends on database. now
I have an array which is dynamic, and key and values are displaying in
I have a function, which is called sometimes with regular, sometimes dynamic arrays. If
I currently have the dynamic array: char *myData[500][10]; //myData is the name of an
I have a 2D dynamic array. I enter a line of 0's after line
I have a struct with a dynamic array inside of it: struct mystruct {
I have a Dynamic Data Entities Web Application that uses a database with GUIDs
I'm building an application which will have dynamic allocated objects of type A each

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.