Laravel array_diff()作为更改日志

时间:2018-11-07 18:39:38

标签: php laravel

我的数据库中有两个json字符串,其中包含我要相互比较的数据,以显示在活动日志中,以查看用户对条目进行了哪些更改:old_properties和{{1} }。

这是我当前的控制器:

properties

这是我的刀刃:

$properties = json_decode($log->properties, true);
$old_properties = json_decode($log->old_properties, true);
$changes = array_diff($properties, $old_properties);

例如,如果我的数据是:

@foreach ($changes as $key => $value)
    {{$key}}: {{$value}}<br>
@endforeach

我从$ changes中看到的全部是:old_properties: 'name' => 'Matthew', 'state' => 'Oregon' properties: 'name' => 'Samuel', 'state' => 'Oregon'

我要显示的是:name: Samuel,或通过其他方式显示旧属性以及在同一行中将其更改为什么。

2 个答案:

答案 0 :(得分:2)

您似乎可以执行以下操作:

@foreach ($changes as $key => $value)
    {{$key}}: {{$old_properties[$key]}} -> {{$value}}<br>
@endforeach

答案 1 :(得分:0)

进行Set计算可以解决此问题:

function printKeptAndUpdated ($setMapKeptAndUpdated, $setA, $setB) {
    $changes = [];
    foreach ($setMapKeptAndUpdated as $key => $value) {
        $changes[] = $key . ": " . $setA[$key]  . " -> " . $setB[$key];
    }
    echo("<pre>");
    print_r($changes);
    echo("</pre>");
}

$old_properties = [
    "ape" => "black",
    "cat" => "white",
    "dog" => "black",
    "fox" => "red",
];
$properties = [
    "bear" => "brown",
    "cat" => "white",
    "dog" => "yellow",
    "eagle" => "grey",
    "fox" => "white",
    "giraffe" => "yellow",
];

// Define 2 Set of array, setA is old set data, setB is new set data.
// Both setA and setB should only be one dimensional array
// which elements are always key => value pair only.
$setA = $old_properties;
$setB = $properties;

$setMap = [];
// Set map will do Set calculation in the future,
// and imply those 4 possibility: deleted, added, kept (updated or untouched),
// but only 3 of them are shown: deleted, added, kept.

// 4 possiblility after Set calculation.
$setMapDeleted = [];
$setMapAdded = [];
$setMapKeptAndUpdated = [];
$setMapKeptAndUntouched = [];

// Put $setA in $setMap first.
foreach($setA as $key => $value) {
    $setMap[$key] = "a";
}

// Then put $setB in $setMap too.
foreach($setB as $key => $value) {
    if (array_key_exists($key, $setMap)) {
        $setMap[$key] = "ab";
    } else {
        $setMap[$key] = "b";
    }
}
// So now the $setMap structure looks like this
// (just example, not as same as current data):
//$setMap = [
//    "ape" => "b",
//    "bear" => "ab",
//    "cat" => "a",
//    "dog" => "a",
//    "eagle" => "b",
//];
// Where "a" = setA - setB, "b" = setB - setA, "ab" = setA ∩ setB (intersection)

// Now finish the Set calculation and also separate Set "kept" to set "updated" and Set "untouched".
foreach($setMap as $key => $value) {
    if ($value === "a") {
        $setMapDeleted[$key] = $setA[$key];
    } elseif ($value === "b") {
        $setMapAdded[$key] = $setB[$key];
    } elseif ($value === "ab") {
        if ($setB[$key] === $setA[$key]) {
            $setMapKeptAndUntouched[$key] = $setB[$key];
        } else {
            $setMapKeptAndUpdated[$key] = $setB[$key];
        }
    }
}

printKeptAndUpdated($setMapKeptAndUpdated, $setA, $setB);
// Print the result you want.