【问题标题】:If value exists in array, end, and restart with next array value如果数组中存在值,则结束,并从下一个数组值重新开始
【发布时间】:2012-01-27 16:52:49
【问题描述】:

我整天都在尝试用 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_存在。如果该值已经存在,则不应执行以下代码, 相反,脚本应该从头开始,但第二个值是 数组 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') ");

    ?>

【问题讨论】:

  • 到目前为止你写了什么代码?
  • 我真的不明白你想要做什么。你能举个例子吗?
  • 请重新阅读更新后的问题:)
  • 我还是不明白。为什么你一直在谈论“重新启动脚本,但使用数组的第二个值”???一个简单的循环有什么问题?还不清楚……$acs_exists 是字符串吗?为什么要附加当前 id ($acs_id)? $acs_types1 是什么?

标签: php arrays


【解决方案1】:
foreach ($acs_id as $item) {

  if ($item == $acs_exist) continue;

  // continue with the script (query etc)

}

这就是你所追求的吗?

【讨论】:

  • @MaartenHartman 是$acs_id 和数组吗?你能显示一个print_r()吗?另外,$add_total$done_total 包含什么?
【解决方案2】:

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

// 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, …)
  }
}

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多