print_r()函数将结果返回为简单的“”;数组“;用于diff算法


print_r() function returns result as simple "Array" for diff algorithm

我正在使用一个diff算法(Paul Butler算法),现在我已经开始工作了。我发现我需要mysql_fetch_array()而不是mysql_fetch _object(),它解决了警告和错误消息,但现在当我尝试print_r()显示结果时,结果只是"array",而不是我需要它来显示和比较的数据库中的字段。。。当我调用一个不同的函数时,它似乎会显示字段,但不会对它们进行比较,而是将它们列出两次,并在前面列出Array ( [0] => Array ( [d] => Array ( [0] =>

知道为什么吗?是的,我在谷歌上搜索了这个,查找了一下,花了大约两天的时间试图找出它。

以下是脚本的部分:

    $oldhistory = mysql_query("SELECT history FROM members WHERE id = '$id'") or die(mysql_error());
    $oldhistory = mysql_fetch_array($oldhistory);
    $newhistory = mysql_query("SELECT history FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $newhistory = mysql_fetch_array($newhistory);
    $oldpersonality= mysql_query("SELECT personality FROM members WHERE id = '$id'") or die(mysql_error());
    $oldpersonality = mysql_fetch_array($oldpersonality);
    $newpersonality = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());   
    $newpersonality = mysql_fetch_array($newpersonality);
    $oldappearance = mysql_query("SELECT description FROM members WHERE id = '$id'") or die(mysql_error());
    $oldappearance = mysql_fetch_array($oldappearance);
    $newappearance = mysql_query("SELECT personality FROM profile_edit WHERE userid = '$id'") or die(mysql_error());    
    $newappearance = mysql_fetch_array($newappearance);
    echo "
      <form action='"$PHP_SELF?id=$id&s=a'" method='"post'">
      <table cellpadding='"5'" cellspacing='"1'" border='"0'" align='"center'" bgcolor='"#234904'">
        <tr bgcolor='"#000000'">
         <td>History :: </td>
         <td> 
";
function diffhistory($oldhistory, $newhistory){
foreach($oldhistory as $oindex => $ovalue){
        $nkeys = array_keys($newhistory, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$oldhistory, 'i'=>$newhistory));
    return array_merge(
        diffhistory(array_slice($oldhistory, 0, $omax), array_slice($newhistory, 0, $nmax)),
        array_slice($newhistory, $nmax, $maxlen),
        diffhistory(array_slice($oldhistory, $omax + $maxlen), array_slice($newhistory, $nmax + $maxlen)));
}
function htmlDiffHistory($oldhistory, $newhistory){
    $diffhistory = diffhistory(explode(' ', $oldhistory), explode(' ', $newhistory));
    foreach($diffhistory as $k){
        if(is_array($k))
            $ret .= (!empty($k['d'])?"<del><color=red>".implode(' ',$k['d'])."</color></del> ":'').
                (!empty($k['i'])?"<ins><color=green>".implode(' ',$k['i'])."</color></ins> ":'');
        else $ret .= $k . ' ';
    }
    return $ret;
}
print_r (diffhistory($oldhistory, $newhistory));

您可能只需要使用mysql_fetch_assoc()(或可能使用mysql_fetch_row())从数据库中读取关联或索引的数据,而不是同时使用两者,但我怀疑您的方法存在很大缺陷-首先,您要进行6个查询,其中一个要进行查询。您还将一个值数组传递给一个函数,我怀疑该函数需要两个多值数组进行比较。

试试这个(编辑基于下面的评论):

<?php
  function diff($old, $new){
    $maxlen = 0;
    foreach ($old as $oindex => $ovalue) {
      $nkeys = array_keys($new, $ovalue);
      foreach ($nkeys as $nindex) {
        $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1])
          ? $matrix[$oindex - 1][$nindex - 1] + 1
          : 1;
        if ($matrix[$oindex][$nindex] > $maxlen) {
          $maxlen = $matrix[$oindex][$nindex];
          $omax = $oindex + 1 - $maxlen;
          $nmax = $nindex + 1 - $maxlen;
        }
      } 
    }
    return ($maxlen == 0)
      ? array(array('d'=>$old, 'i'=>$new))
      : array_merge(
          diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
          array_slice($new, $nmax, $maxlen),
          diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
  }
  function htmlDiff($old, $new){
    $ret = '';
    $diff = diff(preg_split('/'s+/', $old), preg_split('/'s+/', $new));
    foreach ($diff as $k) {
      if (is_array($k)) {
          $ret .= (!empty($k['d']) ? "<del style='color:red'>".implode(' ',$k['d'])."</del> " : '')
                . (!empty($k['i']) ? "<ins style='color:green'>".implode(' ',$k['i'])."</ins> " : '');
      } else {
        $ret .= $k . ' ';
      }
    }
    return $ret;
  }
  $query = "
    SELECT
      `m`.`history`, `p`.`history` AS 'p_history',
      `m`.`personality`, `p`.`personality` AS 'p_personality',
      `m`.`description`, `p`.`description` AS 'p_description'
    FROM `members` `m`
    JOIN `profile_edit` `p` ON `p`.`userid` = `m`.`id`
    WHERE `m`.`id` = '".mysql_real_escape_string($id)."'
    LIMIT 1
  ";
  $result = mysql_query($query) or die(mysql_error());
  $data = mysql_fetch_assoc($result);
  echo "
    <form action='"$PHP_SELF?id=$id&s=a'" method='"post'">
      <table cellpadding='"5'" cellspacing='"1'" border='"0'" align='"center'" bgcolor='"#234904'">
        <tr bgcolor='"#000000'">
          <td>History :: </td>
          <td>".htmlDiff(htmlspecialchars($data['history']), htmlspecialchars($data['p_history']))."</td>
        </tr>
        <tr bgcolor='"#000000'">
          <td>Personality :: </td>
          <td>".htmlDiff(htmlspecialchars($data['personality']), htmlspecialchars($data['p_personality']))."</td>
        </tr>
        <tr bgcolor='"#000000'">
          <td>Description :: </td>
          <td>".htmlDiff(htmlspecialchars($data['description']), htmlspecialchars($data['p_description']))."</td>
        </tr>
      </table>
    </form>";

尝试使用var_dump打印,如果它只是用于验证数组内的数据:

echo '<pre>'; var_dump($yourArray); echo'</pre>';