php中数组中某个键值为空的时候,不加入到数组。
$data = array();
if (!empty($_POST['field_id'])) {
foreach ($_POST['field_id'] as $k => $v) {
$data[] = array(
'field_id' => $v,
'choice' => isset($_POST['choice'][$k]) ? $_POST['choice'][$k] : '',
'uid' => $_POST['uid'],
'game_id' => $_POST['game_id'],
'server_id' => $_POST['server_id']
);
}
}
这是我循环格式化的地方,下面是输出的数组数据
Array
(
[0] => Array
(
[field_id] => 1
[choice] => 0
[uid] => 110000110
[game_id] => 2
[server_id] => 2
)
[1] => Array
(
[field_id] => 2
[choice] => 0
[uid] => 110000110
[game_id] => 2
[server_id] => 2
)
[2] => Array
(
[field_id] => 3
[choice] => 1
[uid] => 110000110
[game_id] => 2
[server_id] => 2
)
)
当choiece等于空的时候 [choice] =>没有值的时候不加入到data数组里面怎么实现呢?
------解决思路----------------------
$data = array();
if (!empty($_POST['field_id'])) {
foreach ($_POST['choice'] as $k => $v) {
if($v == '') continue;
$data[] = array(
'field_id' => $_POST['field_id'][$k],
'choice' => $_POST['choice'][$k],
'uid' => $_POST['uid'],
'game_id' => $_POST['game_id'],
'server_id' => $_POST['server_id']
);
}
}
相关文章
相关视频