0

I have an array that comes from a form POST and I'd like to group these together.

My first attempt is running a foreach loop on the array, but I'm unsuccesful in getting the logic to group them. I have the idea of creating a dynamic array using the number from the keys, but I'm unable to grab that number.

Initial array:

["affix-1-order"]=> "1";
["affix-1-type"]=> "Apple";
["affix-1-count"]=> "5";
["affix-3-order"]=> "2";
["affix-3-type"]=> "Orange";
["affix-3-count"]=> "10";
["affix-2-order"]=> "3";
["affix-2-type"]=> "Banana";
["affix-2-count"]=> "3";
["affix-4-order"]=> "4";
["affix-4-type"]=> "Mango";
["affix-4-count"]=> "15";

Expected output:

["1"]=> [{
    ["type"]=> "Apple",
    ["count"]=> "5",
    ["order"]=> "1"
}],
["2"]=> [{
    ["type"]=> "Banana",
    ["order"]=> "3",
    ["count"]=> "3"
}],
["3"]=> [{
    ["type"]=> "Orange",
    ["order"]=> "2",
    ["count"]=> "10",
}],
["4"]=> [{
    ["type"]=> "Mango",
    ["order"]=> "4",
    ["count"]=> "15"
}];
3
  • 2
    you could build your form so that you got the required array in the post with out any manipulation latter on Commented Jan 10, 2014 at 3:12
  • I don't understand? How do you suggest I group a key and 3 values within a form input? Commented Jan 10, 2014 at 3:18
  • name=X[]['type'] name=X[]['order'] name=X[]['count'], the post X array will be what you want Commented Jan 10, 2014 at 3:35

2 Answers 2

3
$array = array(
  "affix-1-order" => "1",
  "affix-1-type" => "Apple",
  "affix-1-count" => "5",
  "affix-3-order" => "2",
  "affix-3-type" => "Orange",
  "affix-3-count" => "10",
  "affix-2-order" => "3",
  "affix-2-type" => "Banana",
  "affix-2-count" => "3",
  "affix-4-order" => "4",
  "affix-4-type" => "Mango",
  "affix-4-count" => "15",
);

$final = array();
foreach($array as $key => $value) {
    preg_match('/affix\-([\d]+)\-(.*)/', $key, $matches);
    // $matches[0] = 'affix-1-order';
    // $matches[1] = '1';
    // $matches[2] = 'order';

    $final[$matches[1]][$matches[2]] = $value;
}

print_r($final);
// Array (
//   [1] => Array ( [order] => 1 [type] => Apple [count] => 5 )
//   [3] => Array ( [order] => 2 [type] => Orange [count] => 10 )
//   [2] => Array ( [order] => 3 [type] => Banana [count] => 3 )
//   [4] => Array ( [order] => 4 [type] => Mango [count] => 15 )
// )
Sign up to request clarification or add additional context in comments.

3 Comments

I ended up modifying the form, but this is a brilliant solution, thanks! :)
No problem @veksen, modifying the form is a much better solution. But its always good to learn a little bit of regex ;)
Regular expressions are definitly my next big leap
2

I would rather follow Dagon's suggestion and modify the form or Ajax request that sends the data.

By using square brackets on the field names you can have PHP do the job for you.

<?php
if(count($_POST))
{
    print_r ($_POST['data']);
    die();
}
?>

<body onload='document.getElementById("post").submit();'>
    <form method="POST" id="post">
        <input type='text' name='data[0][type]'  value='Apple'>
        <input type='text' name='data[0][count]' value='5'>
        <input type='text' name='data[0][order]' value='1'>
        <input type='text' name='data[1][type]'  value='Banana'>
        <input type='text' name='data[1][count]' value='3'>
        <input type='text' name='data[1][order]' value='3'>
        <input type='text' name='data[2][type]'  value='Orange'>
        <input type='text' name='data[2][count]' value='2'>
        <input type='text' name='data[2][order]' value='10'>
        <input type='text' name='data[3][type]'  value='Mango'>
        <input type='text' name='data[3][count]' value='4'>
        <input type='text' name='data[3][order]' value='15'>
    </form>
</body>

output

[0] => Array
        [type] => Apple
        [count] => 5
        [order] => 1
[1] => Array
        [type] => Banana
        [count] => 3
        [order] => 3
[2] => Array
        [type] => Orange
        [count] => 2
        [order] => 10
[3] => Array
        [type] => Mango
        [count] => 4
        [order] => 15

4 Comments

Didn't read enough to see that this was coming from a POSTed form. This would be the better option.
@SamSullivan OTOH my solution falls short of regexps :)
I didn't know this was possible... thank you, you just saved me hours of work :)
So much the better. I actually stumbled onto this in the PHP manual. You can learn a lot by browsing the comments there, if you don't mind the ugliness of their presentation ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.