本文介绍了Php:如何计算两个“相似"数组之间的差异对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组 $tab,它们是表的行"(即您可以通过$tab[$i]->columnname.

I have an array of objects $tab that are "rows" of a table (i.e. you can access each column through "$tab[$i]->columnname.

我有另一个对象数组 $tab_json,它是 AJAX 调用的返回,并且包含,,表的行"(即您可以通过$tab_json[$i]->columnname.

I have another array of objects $tab_json that is the return of an AJAX call, and that contains, too, "rows" of a table (i.e. you can access each column through "$tab_json[$i]->columnname.

两个 数组包含完全相同的列,但我只想猜测"$tab 中的哪些列$tab_json.

Both arrays contain exactly the same colums, but I would just like to "guess" which ones in $tab are not present in $tab_json.

当然我知道 array-intersect 和 array-diff 函数,但它们似乎在对象比较方面效果不佳.除非我错了?

Of course I know array-intersect and array-diff functions, but they do not seem to work well on objects comparison. Unless I'm wrong?

这是一个我想要工作的示例,但有一个 Php 例外:

Here's a sample that I'd like to work, but there's a Php exception:

tab_json = PHP Catchable fatal error:  Object of class stdClass could not be converted to string in sample.php on line 112

只需将其复制粘贴并运行到一个文件中 (php -f filename.php).知道我该怎么做吗?

Just copy paste it and run it into a file (php -f filename.php). Any idea how I should do?

<?php
$tab = array(
    (object)array(
        'id'          => 1,
        'titre'       => "Anchois",
        'attributs'   => array()
    ),
    (object)array(
        'id'          => 4,
        'titre'       => "Jambon",
        'attributs'   => array()
    ),
    (object)array(
        'id'           => 12,
        'titre'        => "La Cabro d'or",
        'attributs'    => array(
            (object)array("id" => 1),
            (object)array("id" => 8)
        )
    )
);


$tab_json = array (
    (object)array(
        'id' => 1,
        'titre' => 'Anchois',
        'attributs' =>
        array (
        ),
    ),
    (object)array(
        'id' => 4,
        'titre' => 'Jambon',
        'attributs' =>
        array (
        ),
    ),
    (object)array(
        'id' => 12,
        'titre' => 'La Cabro d\'or',
        'attributs' =>
        array (
        ),
    ),
    (object)array(
        'id' => 25,
        'titre' => 'Vin rouge ou rosé',
        'attributs' =>
        array (
        ),
    ),
    (object)array(
        'id' => 22,
        'titre' => 'Crème oignons lardons',
        'attributs' =>
        array (
            (object)array(
                'id' => 1,
            ),
            (object)array(
                'id' => 2,
            ),
        ),
    )
);
echo "tab = "; var_export($tab); echo "\n";
echo "tab_json = "; var_export($tab_json); echo "\n";
echo "tab_json = "; var_export(array_diff($tab_json,$tab)); echo "\n";
?>

推荐答案

我已经制作了似乎比较ok"的函数:我连接"两个数组的属性以制作字符串",然后使用 strcmp() 函数返回结果.因此,当值不同时,它们会更改为字符串,而当涉及到子"数组时,它们会转换为 string = "Array" 所以比较 == 0 所以 "sub" 数组被忽略(这正是我想要的).

I've made the function that seems to compare "ok": I "concatenate" the properties of both arrays to make "strings" then use the strcmp() function to return the result.So, when the values are different they're changed to string, and when it comes to "sub" arrays, they're converted to string = "Array" so the comparison == 0 so the "sub" arrays are ignored (which is precisely what I wanted).

它有效.如果你发现一个例子可以告诉我它不起作用,请在评论中与我分享.谢谢!

It works. If you find an example that could show me that it doesn't work, please share it with me in a comment. Thanks!

$difference = array_udiff($tab_json, $tab, function($a, $b) {
    $d=array_diff_assoc(get_object_vars($a), get_object_vars($b));
    if (count($d)>0) {
        $s0='';
        $s1='';
        foreach ($d as $k=>$val) {
            $r0=(string)$val;
            $r1=(string)$b->$k;
            $l0 = mb_strlen($r0);
            $l1 = mb_strlen($r1);
            for (;$l0<$l1;$l0++) {
                $r0=' '.$r0;
            }
            for (;$l1<$l0;$l1++) {
                $r1=' '.$r1;
            }
            $s0.=$r0;
            $s1.=$r1;
        }
        return strcmp($s0,$s1);
    }
    return 0;
});
echo "difference = "; var_export($difference); echo "\n";

这篇关于Php:如何计算两个“相似"数组之间的差异对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 20:12