Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
1  
Walk the original array and when you see that a key has already been set, you'll know that it's time to start to put the data in a new sub-array. –  Aurelio De Rosa Oct 18 '11 at 0:55

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer
    
Oops, this works well if there's only one digit, but as soon as it goes over 9 (lightwattage10 etc) the script fails. –  rotezecke Dec 16 '11 at 4:25
    
changing the preg_match line did the trick: preg_match('/^(.*)(\d+)$/',$key,$matches); to preg_match('/^([^0-9]*)(\d+)$/',$key,$matches); –  rotezecke Dec 16 '11 at 4:55
    
Edited, regex updated –  ghbarratt Jul 28 '12 at 16:04

This is not an exacty answer to you question, but...

You can use arrays in POST variables like so:

<input name="light[1][wattage]" />
<input name="light[1][voltage]" />
<input name="light[2][wattage]" />
<input name="light[2][voltage]" />

will give you:

$_POST['light'] == array(
    1 => array(
        'wattage' => '...',
        'voltage' => '...',
    ),
    2 => array(
        'wattage' => '...',
        'voltage' => '...',
    ),
)
share|improve this answer
    
i like this approach but i would have to rewrite my form, its jQUery and JS validation. i'll use this next time. cheers –  rotezecke Oct 18 '11 at 2:01

Try this:

$outarr = array()
$subarr = array()
$i=0;
foreach($_POST as $key => $val)
{
    //only include keys starting with light:
    if (strpos("light", $key)==0)
    {
        //create a new subarray each time we find a key that starts with "lightwattage":
        if ($i>0 && strpos("lightwattage", $key)==0)
        {
            $outarr[] = $subarr;
        }
        $subarr[$key] = $val;
        $i++;
    }
}
$outarr[] = $subarr;

//$outarr contains what you want here
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.