您好,欢迎访问一九零五行业门户网

php中数组中某个键值为空的时分,不加入到数组

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']
);
}
}
其它类似信息

推荐信息