如果数组中存在值,则结束并使用下一个数组值重新启动

时间:2012-01-27 16:52:50

标签: php arrays

我一直在努力通过PHP实现以下目标:

更新:这是我正在使用的代码,请阅读

之间的消息
  <?php

    include ('acs_params.php'); 

    $acs_value_based = array(25, 30, 35, 40, 45, 50, 60, 70);
    $acs_types = array("$add_total","$done_total");

        print_r($acs_id);

    foreach ( $acs_value_based as &$value ) {
    foreach ( $acs_types as &$counters) { 

    if ($counters >= $value) {    

    if ($acs_types[0] == $counters) {$acs_types1 = '1'; $acs_name = 'Tasker'; $acs_action = 'adding';}
    if ($acs_types[1] == $counters) {$acs_types1 = '2'; $acs_name = 'Completer'; $acs_action = 'completing';}

    if ($value == '25') { $acs_lvl = '1'; $acs_id = '25';}
    if ($value == '30') { $acs_lvl = '2'; $acs_id = '30';}
    if ($value == '35') { $acs_lvl = '3'; $acs_id = '35';}

    $acs_exists = $acs_types1 . $acs_id;

这里应该检查这个数组是否为print_r($ acs_id);包含的值 $ acs_exists。如果该值已存在,则不应执行以下代码,  相反,脚本应该在开头再次开始,然后使用第二个值  数组print_r($ acs_id);.它的值尚未存在,然后继续查询。 (我希望我的目标很明确。)

   mysql_query("INSERT INTO users_log(id, log, userid, date, points, action) VALUES

(id, 'achievement 1', '$username', '$currenttime', '0', '8') ");

    mysql_query("INSERT INTO users_achievements(id, userid, acs_id, date) VALUES

(id, '$username', '$acs_types1$acs_id', '$currenttime') ");

    ?>

2 个答案:

答案 0 :(得分:0)

这听起来很简单,但我会试一试:

// if $acs_exist is not in the array $acs_id, execute script, otherwise do nothing
if(!in_array($acs_exist, $acs_id)) {
  // do whatever you want 
}

<击>

我还在黑暗中戳戳......

foreach($acs_id => $id) {
  if($acs_id == $acs_exist) { // already exists
  } else { // does not exist yet, insert into db
    // your code: INSERT INTO table (column, …) VALUES(:acs_id, …)
  }
}

答案 1 :(得分:0)

foreach ($acs_id as $item) {

  if ($item == $acs_exist) continue;

  // continue with the script (query etc)

}

这就是你要追求的吗?