Reputation: 7003
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"
}];
Upvotes: 0
Views: 64
Reputation: 8661
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
Upvotes: 2
Reputation: 20486
$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 )
// )
Upvotes: 3