欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-01 13:58:29
...
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']
);
}
}
php中数组中某个键值为空的时分,不加入到数组

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频