Home  >  Article  >  Backend Development  >  php中数组中某个键值为空的时分,不加入到数组

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

WBOY
WBOYOriginal
2016-06-13 12:26:59786browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn