这是我的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我想做的是。将有5个数组,我需要根据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) ?>
但是这只会消除空值
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);