hunterstudio.biz
- 6010 -

The intersection of several arrays

In one of the exercises, you had to find the intersection of several arrays, that is, the elements common to several arrays. Easy ! custom php application development offers the array_intersect function which is used for just that! Except that this function takes as parameters the arrays in which to find the intersection.

And what I have on my side is a (big) table that contains all my tables to test. And depending on the dataset, I don't know how many I have, so can't write

This is where the "..." decomposition operator comes in, to decompose my big table into all of these elements

Block and break your while Several times it was necessary to use while during Advent of Code. And while I wasn't a big fan, I use while (true) more and more. But it remains "dangerous" to code because as long as the code is not finalized, each test can lead to an infinite loop ... It is therefore interesting to use a "blocker"

Here, I think there won't be 1,000+ possible iterations, so if I hit 1000, I "break" my while and avoid my infinite loop.

Likewise, with a while true, you will need to use "break" to exit the loop once the stop condition is reached. The stop condition is therefore moved directly in the logic only in the parentheses of the while. In use, I find it more readable.

Once the code for my while is finalized, I can of course remove this blocker.

Functions linked to tables

PHP offers many functions available for manipulating arrays. Here are a few that I used regularly during Advent of Code:

array_unique: Keep only one occurrence of each element of the array (sorting of duplicates)
str_split: Transform a character string into an array
array_intersect: The intersection of the elements of several arrays (the common elements)
array_slice: Split an array and keep only a portion
array_search: Find the key of a specific element in an array
array_count_values: Count the occurrences of each element
array_merge: Merge arrays
XXsort: Sort an array, the Xs are to be replaced according to the type of sort desired

It's hard to know them all ... But it's a good practice, when working with an array in PHP, to ask yourself if there isn't already a function that can do what you want to do.

Web

  1. 2 Sept. 2021Help Wanted: PHP Development Company is Looking for Skilled Workers829
  2. 5 Août 2021A Programmer's Guide to PHP386
  3. 14 Mai 2021So what is web development in 2021 ?2029
  4. 14 Avril 2021A perfect means of goal management3089
  5. 9 Fév. 2021Manipulating arrays with callback functions and Collections1181
  6. 9 Janv. 2021Keep up to date with the latest Php releases1156