Home > Article > Backend Development > php中数组中某个键值为空的时分,不加入到数组
php中数组中某个键值为空的时候,不加入到数组。
<br /> $data = array();<br /> if (!empty($_POST['field_id'])) {<br /> foreach ($_POST['field_id'] as $k => $v) {<br /> $data[] = array(<br /> 'field_id' => $v, <br /> 'choice' => isset($_POST['choice'][$k]) ? $_POST['choice'][$k] : '', <br /> 'uid' => $_POST['uid'],<br /> 'game_id' => $_POST['game_id'],<br /> 'server_id' => $_POST['server_id']<br /> ); <br /> } <br /> } <br />
<br />Array<br />(<br /> [0] => Array<br /> (<br /> [field_id] => 1<br /> [choice] => 0<br /> [uid] => 110000110<br /> [game_id] => 2<br /> [server_id] => 2<br /> )<br /><br /> [1] => Array<br /> (<br /> [field_id] => 2<br /> [choice] => 0<br /> [uid] => 110000110<br /> [game_id] => 2<br /> [server_id] => 2<br /> )<br /><br /> [2] => Array<br /> (<br /> [field_id] => 3<br /> [choice] => 1<br /> [uid] => 110000110<br /> [game_id] => 2<br /> [server_id] => 2<br /> )<br /><br />)<br /><br />
$data = array();<br />if (!empty($_POST['field_id'])) {<br /> foreach ($_POST['choice'] as $k => $v) {<br /> if($v == '') continue;<br /> $data[] = array(<br /> 'field_id' => $_POST['field_id'][$k], <br /> 'choice' => $_POST['choice'][$k], <br /> 'uid' => $_POST['uid'],<br /> 'game_id' => $_POST['game_id'],<br /> 'server_id' => $_POST['server_id']<br /> ); <br /> } <br />}