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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:42:28+00:00 2026-05-26T04:42:28+00:00

Thank you for any help. This one has me stumped. We start with this:

  • 0

Thank you for any help. This one has me stumped. We start with this:

Array
(
[numCols] => 8
[timePointLabel1] => Week1
[timePointLabel4] => Week2
[timePointLabel7] => Week3
[0] => Array
    (
        [Location0_name] => Name1
        [colText01] => 2
        [colText07] => 4
    )
[1] => Array
    (
        [Location1_name] => Name2
        [colText11] => 9
        [colText14] => 7
    )
)

I want the NNN in the timePointLabelsNNNs to go in numeric order, starting at 0. Currently the NNNs are 1, 4, 7.

We also have an array within an array. The N in the colTextN fields are related to the time point labels. E.g. timePointLabel4 is associated with colText04, colText14, and any other colText that ends in a single digit of 4.

I figured out how to dynamically reorder the NNNs and so I am getting this:

Array
(
[numCols] => 8
[timePointLabel0] => Week1
[timePointLabel1] => Week2
[timePointLabel2] => Week3
[0] => Array
    (
        [Location0_name] => Name1
        [colText01] => 2
        [colText07] => 4
    )
[1] => Array
    (
        [Location1_name] => Name2
        [colText11] => 9
        [colText14] => 7
    )
)

See the NNNs in the timePointLabelNNNs are now 0, 1, 2.

How did I do this? With this code:

$timePointLabelCount = preg_grep("/^timePointLabel(\d)+$/",array_keys($this->data));

// go through the time point label array and create a new array to use
// it is assigned the correct order of keys, starting with 0 (since arrays start with 0 anyway)
foreach ($timePointLabelCount as $timePointCustom) {
    $timePointCustomArray[] = $this->data[$timePointCustom];
    unset($this->data[$timePointCustom]);
}

$timePointNum = 0;
// insert the correct timepoint data, in the correct order, into the array
foreach ($timePointCustomArray as $setTimePointData) {
    $this->data['timePointLabel' . $timePointNum] = $setTimePointData;
    $timePointNum++;
}

But the colTexts are still an issue. For the N values in colTextN, anything that is 1 should now be a 0, the 4 should now be a 1, and the 7s should now be a 2.

So I want my final array to look like this:

Array
(
[numCols] => 8
[timePointLabel0] => Week1
[timePointLabel1] => Week2
[timePointLabel2] => Week3
[0] => Array
    (
        [Location0_name] => Name1
        [colText00] => 2
        [colText02] => 4
    )
[1] => Array
    (
        [Location1_name] => Name2
        [colText10] => 9
        [colText11] => 7
    )
)

I suspect this will require some clever usage of a foreach loop.

  • 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-26T04:42:29+00:00Added an answer on May 26, 2026 at 4:42 am

    You can not rename keys w/o tainting the order in the same array, but you could build a new array (and unset the previous one later on):

    $i = 0;
    $search = 'timePointLabel';
    $reordered = array();
    foreach($array as $key => &$value)
    {
       if (0 === strpos($key, $search))
       {
           $key = $search . $i++;
       }
       $reordered[$key] =& $value;
    }
    unset($value);
    $array = &$reordered;
    unset($reordered);
    print_r($array);
    

    Demo

    Edit: To solve this for the sub-keys, you need to obtain the mapping of the digits and apply those the same way on the sub-array. The principle is actually identical + using the map:

    $i = 0;
    $search = 'timePointLabel';
    $subsearch = 'colText';
    $reordered = array();
    $map = array();
    foreach($array as $key => &$value)
    {
       if (0 === strpos($key, $search))
       {
            $digit = NULL;
            sscanf($key, $search.'%d', $digit);
            if (NULL === $digit || $digit > 9) throw new Exception(sprintf('Invalid Key %d.', $key));
            $map[$digit] = $i;
            $key = $search . $i++;
       }
       elseif (is_int($key) && is_array($value))
       {
            $subreordered = array();
            foreach($value as $subkey => &$subvalue)
            {
                if (0 === strpos($subkey, $subsearch))
                {
                    $lastPos = strlen($subkey)-1;
                    $last = $subkey[$lastPos];
                    if (isset($map[$last]))
                    {
                        $subkey[$lastPos] = $map[$last];
                    }
                }
                $subreordered[$subkey] = &$subvalue;
            }
            unset($subvalue);
            $value =& $subreordered;
            unset($subreordered);
       }
       $reordered[$key] =& $value;
    }
    unset($value);
    unset($map);
    $array = &$reordered;
    unset($reordered);
    print_r($array);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has me stumped, and none of the other related questions here have
This one has got me stumped. I am currently rendering a form using ASP
Regex is absolutely my weak point and this one has me completely stumped. I
Thanks for any help in advance, I can't wrap my SQL skills around this
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Okay, I'm thoroughly stumped on this one. I'm trying to build a menu of
Pointless Dribble Okay This is another weird one from me, i want to thank
I am using c# .net. Thanks in advance for any help. I have searched
Is there any way to get available values for SET field in table? Thank
(This is more of a curiousity question than any pending disaster :D ) So

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.