Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to compare arrays in PHP

1 Answer

0 votes
$array1 = ["php", "java", "c", "c#"];
$array2 = ["c++", "php", "rust", "c"];

// 1 Find values in $array1 that are NOT in $array2
$diff1 = array_diff($array1, $array2);
echo "In array1 but not in array2:\n";
print_r($diff1);

// 2 Find values in $array2 that are NOT in $array1
$diff2 = array_diff($array2, $array1);
echo "\nIn array2 but not in array1:\n";
print_r($diff2);

// 3 Find common values between arrays
$common = array_intersect($array1, $array2);
echo "\nCommon values:\n";
print_r($common);

// 4 Check if arrays are exactly equal (same values & order)
$isEqual = ($array1 === $array2);
echo "\nArrays are exactly equal? " . ($isEqual ? "Yes" : "No") . "\n\n";

// 5 Check if arrays have the same values regardless of order
$isSameValues = (count(array_diff($array1, $array2)) === 0 && count(array_diff($array2, $array1)) === 0);
echo "Arrays have the same values (order ignored)? " . ($isSameValues ? "Yes" : "No") . "\n";

 
 
/*
run:
 
In array1 but not in array2:
Array
(
    [1] => java
    [3] => c#
)

In array2 but not in array1:
Array
(
    [0] => c++
    [2] => rust
)

Common values:
Array
(
    [0] => php
    [2] => c
)

Arrays are exactly equal? No

Arrays have the same values (order ignored)? No
 
*/

 



answered Nov 13, 2025 by avibootz

Related questions

2 answers 199 views
1 answer 185 views
185 views asked Jun 5, 2023 by avibootz
2 answers 214 views
2 answers 185 views
3 answers 306 views
306 views asked Jan 25, 2022 by avibootz
3 answers 300 views
...