Current Location: Home> Latest Articles> Visualization tool to view the difference effect of array_diff()

Visualization tool to view the difference effect of array_diff()

M66 2025-06-06

In PHP, array_diff() is a very practical function that compares differences in arrays, returning values ​​that exist in the first array but not in other arrays. This is very common in scenarios such as data comparison, permission filtering, log analysis, etc. However, code output is often not intuitive, especially when the array content is large or the structure is complex. This article will introduce how to view the difference effect of array_diff() more intuitively through visualization tools.

What is array_diff()?

First, let’s briefly review the usage of array_diff() .

 <?php
$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "grape"];
$result = array_diff($array1, $array2);

print_r($result);
?>

The output result is:

 Array
(
    [0] => apple
    [2] => cherry
)

array_diff() will return an array containing the elements in $array1 that are not in $array2 , retaining the original key name.

Why do you need a visualization tool?

During the development process, if we only compare a few arrays simply, the console output is enough. But if:

  • The array is very large (such as thousands of elements);

  • Complex structure (necked, multi-dimensional);

  • Want to make more intuitive demonstrations (such as writing documents to colleagues or writing documents);

At this time, a graphical way of displaying these differences is needed. Doing so will not only reduce misunderstandings, but will also help locate problems faster.

Use the online visualization tool: Diff Viewer + JSON

We can convert the array to JSON format and then use the visual diff tool for comparison. Here is a recommended method:

Step 1: Convert PHP array to JSON

Use json_encode() to convert an array into a JSON string:

 <?php
$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "grape"];

$diff = array_diff($array1, $array2);

file_put_contents("array1.json", json_encode($array1, JSON_PRETTY_PRINT));
file_put_contents("array2.json", json_encode($array2, JSON_PRETTY_PRINT));
file_put_contents("diff.json", json_encode($diff, JSON_PRETTY_PRINT));
?>

Step 2: Use the online JSON diff tool

Open a JSON visual comparison website, for example:

Upload array1.json and array2.json , or paste the content directly in. The tool highlights which fields are different, helping us understand the differences in arrays clearly.

You can also display the final results based on diff.json for easy review by team members.

Advanced gameplay: Self-built PHP visual page

If you don't want to go to a third-party website every time, you can also build a simple visual page yourself.

 <?php
$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "grape"];

$diff = array_diff($array1, $array2);

?>
<!DOCTYPE html>
<html>
<head>
    <title>Array Diff Viewer</title>
    <style>
        body { font-family: Arial; margin: 20px; }
        pre { background: #f4f4f4; padding: 10px; }
        .diff { color: red; }
    </style>
</head>
<body>
    <h2>Original array1</h2>
    <pre><?php echo json_encode($array1, JSON_PRETTY_PRINT); ?></pre>

    <h2>Original array2</h2>
    <pre><?php echo json_encode($array2, JSON_PRETTY_PRINT); ?></pre>

    <h2 class="diff">Difference results</h2>
    <pre><?php echo json_encode($diff, JSON_PRETTY_PRINT); ?></pre>
</body>
</html>

Save this code as array_diff_viewer.php , and then access it to view the effect. For example:

 http://m66.net/array_diff_viewer.php

Summarize

By combining array_diff() with visualization tools, we can handle array differences more efficiently. Especially in debugging multidimensional data, log comparison, and permission control, using tools such as JSON Diff will make the development process clearer and collaboration smoother. You can choose to use online tools or build your own lightweight comparison page as needed, which is flexible and practical.

I hope this article can help you better understand and use array_diff() , while improving your data processing experience in your project.

If you are interested in more PHP visualization tools, please visit:

?? m66.net/tools

Happy coding! ????????