doubo9799 2012-07-19 09:09
浏览 57
已采纳

php array_diff没有给出结果

Could someone please take a look and let me know what i'm doing wrong... What i'm trying to acheive is, in the Database i have:

AGENT_REF = 1
AGENT_REF = 2
AGENT_REF = 3
AGENT_REF = 4
AGENT_REF = 5

In a file uploaded that parses information

$blmarArray = array($agentref);

AGENT_REF = 1
AGENT_REF = 2
AGENT_REF = 3
AGENT_REF = 5

What i'd like to happen is to extract the AGENT_REF from the database that ISN'T in the file uploaded, in this case AGENT_REF = 4 so i can DELETE it from mysql.

$sql_archeck = mysql_query("SELECT `AGENT_REF` FROM `eprentals`");
$archeck = mysql_fetch_array($sql_archeck);
$sqlarArray = array($archeck);
$combyArrayDiff = array_diff($blmarArray, $sqlarArray);
print_r ($combyArrayDiff);

All i'm getting is the last or first AGENT REF from the database and not the ones that arn't present in the db. (In the db there is 11 but the file uploaded only 8 are present, so i'd like to delete (SHOW) the ones that are in the DB that have been removed from the file uploaded)

Hope you can offer me some guidance into where i'm wrong with this and I Thank you for your time and appriciate your help!

  • 写回答

1条回答 默认 最新

  • dpklt4291 2012-07-19 09:14
    关注

    from php manual:

    Returns an array containing all the entries from array1 that are not present in any of the other arrays.

    so insted of array_diff($blmarArray, $sqlarArray) you should use array_diff($sqlarArray, $blmarArray)

    = the big array as first parameter, and the samller array as 2nd parameter


    you also need to fetch all rows from the database, not just the first

    $query = "SELECT `AGENT_REF` FROM `eprentals`";
    $resource = mysql_query($query);
    $sqlarArray = array();
    while($row = mysql_fetch_array($sql_archeck))
    {
        $sqlarArray[] = $row['AGENT_REF'];
    }
    $combyArrayDiff = array_diff($sqlarArray, $blmarArray);
    
    /* debug result */
    echo "<p><b>SQL list:</b> " . implode(', ', $sqlarArray) . "</p>";
    echo "<p><b>Uploaded list:</b> " . implode(', ', $blmarArray) . "</p>";
    echo "<p><b>Diff list:</b> " . implode(', ', $combyArrayDiff ) . "</p>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题