When a key value in an array in PHP is empty, it is not added to the array.

Source: Internet
Author: User
Tags addall
$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 loop the formatting, the following is the output of the array data
Array (    [0] = = Array        (            [field_id] = 1            [Choice] = 0            [UID] = 110000110            [game_id] =& Gt 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 equals null when [choice] + does not add to the data array when there is no value how to implement it?


Reply to discussion (solution)

[1] = = Array
(
[FIELD_ID] = 2
[Choice] =
[UID] = 110000110
[GAME_ID] = 2
[SERVER_ID] = 2
)
In situations like this, 0 and 1 are valued.

The data in your sample does not have [choice] =
If so, do you want to choice the item or the whole group?

Do you have a $_post[' choice ' loop, which is not easier than a loop of $_post[' field_id ')?

The data in your sample does not have [choice] =
If so, do you want to choice the item or the whole group?

Do you have a $_post[' choice ' loop, which is not easier than a loop of $_post[' field_id ')?



Array
(
[0] = = Array
(
[FIELD_ID] = 1
[Choice] = 0
[UID] = 110000110
[GAME_ID] = 2
[SERVER_ID] = 2
)

[1] = = Array
(
[FIELD_ID] = 2
[Choice] =
[UID] = 110000110
[GAME_ID] = 2
[SERVER_ID] = 2
)

[2] = = Array
(
[FIELD_ID] = 3
[Choice] =
[UID] = 110000110
[GAME_ID] = 2
[SERVER_ID] = 2
)

)

Probably is such an effect, does not have the value not to have, shows like this
Array
(
[0] = = Array
(
[FIELD_ID] = 1
[Choice] = 0
[UID] = 110000110
[GAME_ID] = 2
[SERVER_ID] = 2
)
)
Only the choice has no value, the entire array is not.

$data = Array (), if (!empty ($_post[' field_id ')) {   foreach ($_post[' Choice '] as $k = + $v) {      if ($v = = ") continu e;      $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 = = ") Conti Nue;      $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 moderator, but I use the following batch add, if you have selected the will prompt failure, TP inside AddAll How to determine whether it has been added?
$Choice =d (' Myselect '),//$choices = $Choice->addall ($data), if ($Choice->addall ($data)) {$back [' status ']= ' Success '; $back [' msg ']= ' your bets have been successful! '; $back [' url ']= ' game '; $this->back ($back); }else{$back [' Status ']= ' error '; $back [' msg ']= ' bet failed! Do not repeat the bet Oh! '; $back [' url ']= ' game '; $this->back ($back); exit;}}

if ($Choice->addall ($data, Array (), True)) {} Look at the MoD default is False, changing to true will always change the value of the database, it makes no sense.
if ($Choice->addall ($data, Array (), false)) {} How can you tell if the database is already in existence, and what is the best way for you predecessors?

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.