The program logic has some questions, please help

Source: Internet
Author: User
There is a problem with the program logic, please prawn help
 
  $arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
);

$_check_id = 0;
if ($arr) {
foreach ($arr as $v) {
?>


}
}?>


The initial state of the program is up.
Note that $_check_id is three states, the first is 0, that is $_check_id=0, the second is a value, but not equal to the ID in the array, that is $_check_id=2, the third State is: $_check_id is equal to the ID in the array

Now requires that the $_check_id be compared to the ID in the array at the time of the loop
If equal, the radio in input is selected, that is, the state is checked
If not equal, the first radio that is looped out is selected as checked

Of course, can also take out the loop body to achieve, as long as the above problems can be solved, think for two or three hours, trouble everyone to give a thought it

Of course, the array may also be empty, this does not have to judge, very good solution, now the trouble is how to solve the above problem, think of a half-day, no clue, head big.
------Optimal Solution--------------------


$arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
2 = = Array (' ID ' =>14, ' name ' = ' No.3 '),
3 = = Array (' ID ' =>15, ' name ' = ' No.4 '),
);

$_check_id = 14;
$found = false;
$htmls = Array ();
if ($arr) {
foreach ($arr as $v) {
$input _pre = << <>

EOT;
$input _post = << <>
/>{$v [' Name ']}
EOT;
if (! $found && $_check_id = = $v [' id ']) {
$found = true;
$input _pre. = ' checked ';
}
$htmls [] = Array ($input _pre, $input _post);
}
}
$htmlstr = ";
foreach ($htmls as $html) {
if (! $found) {
$html [0]. = ' checked ';
$found = true;
}
$htmlstr. = Implode ("', $html);
}
Echo $htmlstr;

can be executed directly.
------Other Solutions--------------------
 
  $arr = Array (
0 = = Array (' ID ' =>12, ' name ' = ' # '),
1 = = Array (' ID ' =>13, ' name ' = ' No.2 '),
2 = = Array (' ID ' =>14, ' name ' = ' No.3 '),
);

$_check_id = 0;
if ($arr) {
foreach ($arr as & $v) {
if ($_check_id = = $v [' id ']) {
$v [' check '] = ' checked ';
$_check_id =-1;
}
}
if ($_check_id >=0) $arr [0][' check '] = ' checked ';
foreach ($arr as $v) {
?>
/>
}
}?>

------Other Solutions--------------------

foreach ($arr as $key = = $v) {
if ($key = = $_check_id) {
Here's checked.
Continue
}
......
  • Related Article

    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.