【问题标题】:print_r() function returns result as simple "Array" for diff algorithmprint_r() 函数将结果作为 diff 算法的简单“数组”返回
【发布时间】:2012-02-08 15:21:46
【问题描述】:

我正在使用 diff 算法 (the Paul Butler one),我现在开始使用它。我发现我需要 mysql_fetch_array() 而不是 mysql_fetch_object(),它解决了警告和错误消息,但是现在当我尝试 print_r() 显示结果时,结果只是“数组”而不是数据库中的字段我需要它来显示和比较......当我调用不同的函数时,它似乎显示了字段,但不比较它们而不是列出它们两次并在它之前加上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));

【问题讨论】:

    标签: php mysql arrays diff


    【解决方案1】:

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

    试试这个(EDITED 基于下面的 cmets):

    <?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>";
    

    【讨论】:

    • 啊我不知道我能做到这一点......我所知道的关于编码的几乎所有知识都是我从分解网站目前使用的代码中学到的。我会尝试使用这个,只有一个问题,我将如何指定 diff 函数以仅使用历史记录部分?
    • 那么历史部分只是一个值 - 你不需要特殊的 diff 函数,你可以测试(基于上面的查询代码)$result['history'] == $result['p_history']
    • 那么用上面的替换整个 diff 代码?抱歉问了这么多问题,我只是想确保我理解正确:)
    • 您使用上面的代码以及这两个函数 - 我没有将它们包含在上面的代码中以节省空间。
    • 非常感谢您在这方面对我的耐心,顺便说一句...我知道与初学者打交道可能会令人沮丧 XD 我输入了上面的代码,并且一定会记下来以备将来使用...现在的结果虽然给了我“Array ( [0] => Array ( [d] => [i] => ) [1] => [2] => [3] => [4] => Array ( [ d] => [i] => ) ) "
    【解决方案2】:

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

    echo '&lt;pre&gt;'; var_dump($yourArray); echo'&lt;/pre&gt;';

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2017-03-09
      • 2018-11-13
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      相关资源
      最近更新 更多