这是我的POST数组PHP
Array
(
[book_count] => 2
[book_code_1] => 230
[book_name_1] => 20
[book_code_2] => 210
[book_name_2] => 40
[book_code_3] =>
[book_name_3] => 60
[book_code_4] =>
[book_name_4] => 80
[book_code_5] =>
[book_name_5] => 90
)
在上面的数组中,我得到了book_count。即2 我想做的是。 将有五个数组,我需要获得相对于book_count的数组值。
换句话说 - 在上面的例子中,书数是2,我需要得到
[book_code_1] => 230
[book_name_1] => 20
[book_code_2] => 210
[book_name_2] => 40
无论值如何,我都需要消除其他数组(在本例中为3到5)。我怎样才能做到这一点?
我试过了
<?php $new_array = array_filter($_POST) ?>
但这只是消除了空值。
答案 0 :(得分:1)
<强> Demo 强>
你可以通过下面的简单迭代来做到这一点;
$arr = array(
"book_count" => 2,
"book_code_1" => 230,
"book_name_1" => 20,
"book_code_2" => 210,
"book_name_2" => 40,
"book_code_3" => "",
"book_name_3" => 60,
"book_code_4" => "",
"book_name_4" => 80,
"book_code_5" => "",
"book_name_5" => 90
);
$result = array();
for ($i = 1; $i <= $arr["book_count"]; $i++) {
$result["book_code_" . $i] = $arr["book_code_" . $i];
$result["book_name_" . $i] = $arr["book_name_" . $i];
}
var_dump($result);