这是我的php函数
public function pointComplete() {
$inputs = Input::all();
print_r("<pre>");
print_r($inputs);
print_r("</pre>");
}
输出如下:
Array
(
[_token] => 2lIrWksIlJHjHASKAS0UDDSYLtcYwsDYCuxhjQ32
[complete] => Complete
[post_id_9] => 1
[post_id_10] =>
[post_id_12] => 2
[post_id_13] =>
[post_id_14] =>
)
我想得到这个php数组:
array (
"post1" => array(
"id" => "9",
"point" => "1"
),
"post2" => array(
"id" => "12",
"point" => "1"
)
);
答案 0 :(得分:1)
One way you could go about this is to just loop through the input, and parse the key. Then put the result you parsed into another array.
$post_id = 1;
$posts = array();
foreach ($inputs as $key => $value) {
$explode = explode('post_id_', $key);
if (count($explode) == 2 && $value !== null) {
$id = $explode[1];
$posts['post'.$post_id] = array('id' => $id, 'value' => $value);
$post_id++;
}
}
Array
(
[post1] => Array
(
[id] => 9
[value] => 1
)
[post2] => Array
(
[id] => 12
[value] => 2
)
)
Although I am really just taking this question at face value here. There is probably a much easier way to achieve what you're trying to do.