数组Diff不查找数组

时间:2012-05-21 18:31:23

标签: php arrays array-difference

我有一个应该比较2个数组的函数。 Array diff发出警告,第一个参数不是数组....也可以打开任何更好的方法来编写这个函数。我只是喜欢这个。谢谢!

function changeLog(){
include('../includes/conn.inc.php'); 
//select object and make an array with each current value 
    $stmt = $mysql->prepare ("SELECT * FROM table WHERE id=?");
    $stmt->bind_param("i", $_POST['id']);
    $OK = $stmt->execute();
    $stmt->bind_result(
        $id,
        $name,
        $created,
        $edited,
        $owner
    );
while($row = $stmt->fetch()) {
    $array1 = array(
        'name' => $name,
        'owner' => $owner 
        );}
    $stmt->close(); 

//$array1 now holds current values for each field
//now grab the post values from the update and stick them into array2

        $name= $_POST['name'];
        $owner= $owner;

    $array2 = array(
        'name' => $name,
        'owner' => $owner 
        );

//$array2 now holds post values for each field in the update
//check the arrays and spit out the differences

    $result = array_diff($array1, $array2);
//strip the values and just output the array keys

$dbInput =(array_keys($result));

        foreach($dbInput as $i){
            $owner=  'use'.$_SESSION['i'];
            $sql = 'INSERT INTO history (id, created, edited, owner, parent, body) 
                    VALUES (NULL,NOW(),NOW(),?,?,?)';
            $stmt = $mysql->stmt_init();
            if ($stmt->prepare($sql)) { 
                $stmt->bind_param('sss', $owner, $_POST['id'], $i);
                $OK = $stmt->execute();}
                $stmt->close(); 

        }
}// end changeLog

1 个答案:

答案 0 :(得分:0)

尝试将它编码得更像这样,看看它是否告诉你任何事情:

if($row = $stmt->fetch()) {
   $array1 = array('name' => $name, 'owner' => $owner);
   //$array1 now holds current values for each field 
   //now grab the post values from the update and stick them into array2
   $name= $_POST['name'];
   $owner= $owner;
   $array2 = array('name' => $name,         'owner' => $owner          );
  //$array2 now holds post values for each field in the update 
  //check the arrays and spit out the differences
  $result = array_diff($array1, $array2); 
 // ...
} else {
      echo "no result returned";
}