Apa itu operasi selisih dalam suatu himpunan? operasi selisih dalam suatu himpunan adalah cara untuk mendapatkan isi satu himpunan yang tidak dimiliki oleh himpunan lain yang diselisihkan kepada himpunan yang pertama, atau secara sederhananya adalah mengurangi isi himpunan pertama dengan isi himpunan yang kedua sehingga isi himpunan yang pertama tidak ada yang sama dengan isi himpunan yang kedua, semua isi himpunan yang sama pada himpunan yang pertama dengan isi himpunan yang kedua dibuang. Show
Fungsi untuk melakukan operasi selisih ini adalah dengan array_diff(). Berikut contoh scriptnya, silakan di coding. <?php $array_a=array(10,12,4,37,11); echo "Isi array \$array_a <br>"; print_r($array_a); echo "<br>"; $array_b=array("xy",10,4,5,11); echo "Isi array \$array_b <br>"; print_r($array_b); echo "<br>"; $selisih =array_diff($array_a, $array_b); echo "Isi array \$selisih <br>"; print_r($selisih); ?> Setelah script di atas dieksekusi maka berikut tampilan yang akan terlihat. Fungsi array_diff menghasilkan isi $array_a yang elemennya tidak terdapat pada array $array_b. Contoh di atas memperlihatkan isi dari $selisih adalah 12 dan 37 yang berasal dari elemen $array_a index 1 dan 3. Sekian dulu tutorial dari saya, kalau ada yang belum ngerti boleh ditanyakan kalau ada yang salah mohon untuk direvisi. Kritik dan sarannya serta komentarnya gan baik untuk tulisan ini atau blognya. Semoga tulisan yang sedikit ini bermanfaat bagi kita semua, salam blogger J Created: June-07, 2022 Table of Contents
All sorts of operations are done with arrays with different complexities, and comparison operations are no different. When we compare two values, we intend to understand how similar or dissimilar they are. With numbers (integers and float), characters, and strings, it can be straightforward. However, with arrays, it can become a little bit complicated. Don’t fret because it is easy, and there are different operators that we can use. Whether the comparison is strict or not, there is an operator. This article will consider four operators and three functions that make comparing two arrays easy and how to make it happen with excellent examples. Use the == and != Operators to Compare Two Arrays in PHPThe first operator is the Comparison operations focus on finding the similarities or dissimilarities between two values. So, with the Here, the code uses the
Output:
You can use the It releases a Boolean response, where TRUE represents dissimilarity and FALSE represents similarity.
Output:
These two operators work with associative and multi-dimensional arrays and will only compare the values, not the keys. Use the === and !== Operators to Compare Two Arrays in PHPSimilar to the However, Using the same codes in the previous section, we will make some type changes and use both the
Output:
When we use the
When we use it with associative arrays, we see the same behavior. In the code below, we will compare the two arrays in terms of their value and not keys.
Output:
Use the sort() Function to Compare Two Arrays When Order Is Not Important in PHPFor cases where the order of the element isn’t important, you can use the
Output:
Use the array_diff() Function to Compare Two Arrays and Return the Difference in PHPWe can compare two arrays and return the difference between the two arrays, and do so, we use the
Output:
You have to know
the order you place the arrays in the
And if there is no difference, the code will return an empty array. Use the array_intersect() Function to Compare Two Arrays and Return the Matches in PHPWith the
Output:
Related Article - PHP ArrayHow do I check if two arrays are equal in PHP?Use the sort() function to sort an array element and then use the equality operator. Use array operator ( == ) in case of Associative array. How can I find matching values in two arrays PHP?The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc. How do you compare values in an array?Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method. What is difference between array and [] in PHP?Note: Only the difference in using [] or array() is with the version of PHP you are using. In PHP 5.4 you can also use the short array syntax, which replaces array() with []. Apa itu Array_diff?Fungsi array_diff() berguna untuk membandingkan nilai dari dua (atau lebih) array dan mengembalikan perbedaannya. Fungsi ini membandingkan nilai dari dua (atau lebih) larik dan mengembalikan larik yang berisi entri dari larik1 yang tidak ada dalam larik2 atau larik3, dll.
Apa itu array Function PHP?Array dalam PHP adalah jenis struktur data yang memungkinkan kita untuk menyimpan beberapa elemen dari tipe data yang sama di bawah satu variabel tunggal. Sehingga menghemat upaya kita menciptakan variabel yang berbeda untuk setiap data. Array dibuat menggunakan array () function dalam PHP.
|