您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/3/27 5:40:18发布22次查看
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']
);
}
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product