首頁  >  文章  >  後端開發  >  php中数组中某个键值为空的时分,不加入到数组

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

WBOY
WBOY原創
2016-06-13 12:26:59789瀏覽

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 />


当choiece等于空的时候   [choice] =>没有值的时候不加入到data数组里面怎么实现呢?

------解决思路----------------------
$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 />}
 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn