When a key value in the array in php is null, it is not added to the array.
$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']); } }
This is where I cyclically formatted it. below is the output array data
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 ))
When choiece is null, [choice] => if no value exists, how can this problem be solved without adding it to the data array?
Reply to discussion (solution)
[1] => Array
(
[Field_id] => 2
[Choice] =>
[Uid] = & gt; 110000110
[Game_id] => 2
[Server_id] => 2
)
In such a case, 0 and 1 have values.
The data in your example does not contain [choice] =>
If so, do you only need choice items or do not need the whole group?
Isn't it easier for you to compare the $ _ POST ['choice'] loop with the $ _ POST ['Field _ id'] loop?
The data in your example does not contain [choice] =>
If so, do you only need choice items or do not need the whole group?
Isn't it easier for you to compare the $ _ POST ['choice'] loop with the $ _ POST ['Field _ id'] loop?
Array
(
[0] => Array
(
[Field_id] => 1
[Choice] => 0
[Uid] = & gt; 110000110
[Game_id] => 2
[Server_id] => 2
)
[1] => Array
(
[Field_id] => 2
[Choice] =>
[Uid] = & gt; 110000110
[Game_id] => 2
[Server_id] => 2
)
[2] => Array
(
[Field_id] => 3
[Choice] =>
[Uid] = & gt; 110000110
[Game_id] => 2
[Server_id] => 2
)
)
This is probably the effect. if there is no value, it will not be displayed.
Array
(
[0] => Array
(
[Field_id] => 1
[Choice] => 0
[Uid] = & gt; 110000110
[Game_id] => 2
[Server_id] => 2
)
)
If choice has no value, the entire array will not be used.
$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'] ); } }
$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'] ); } }
Thank you for the moderator. However, if I have already selected the batch addition below, an error will be prompted. in tp, how can I determine whether the addall has been added?
$ Choice = D ('myselect'); // $ choices = $ Choice-> addAll ($ data); if ($ Choice-> addAll ($ data )) {$ back ['status'] = 'success'; $ back ['MSG '] = 'Your bet has been successful! '; $ Back ['URL'] = 'game'; $ this-> back ($ back);} else {$ back ['status'] = 'error '; $ back ['MSG '] = 'betting failed! Don't bet again! '; $ Back ['URL'] = 'game'; $ this-> back ($ back); exit ;}}
If ($ Choice-> addAll ($ data, array (), true) {} saw that mod is false by default. changing to true will change the database value all the time, that makes no sense.
If ($ Choice-> addAll ($ data, array (), false) {} in this case, how can we determine whether a database already exists? do you have any good solutions?