John Davidson

php - Does this array algorithm have a name?


I'm trying to match up two different types of users (males and females), maybe I'm going about this the wrong way but it's like this:

Users of type male: [A, B, C, D, E ...]

Users of type female: [1, 2, 3, 4, 5 ...]

Both arrays will always be the same size. I want to match the first type with the second type (each one has a unique identifier). The output should be like below multidimensional array where the key is male and value is female:

The items are objects of users from a DB.

 1: [
[A, 1], [B, 2], [C, 3], [D, 4], [E, 5]
2: [
[A, 2], [B, 3], [C, 4], [D, 5], [E, 1]
3: [
[A, 3], [B, 4], [C, 5], [D, 1], [E, 2]
4: [
[A, 4], [B, 5], [C, 1], [D, 2], [E, 3]
5: [
[A, 5], [B, 1], [C, 2], [D, 3], [E, 4]

If it helps, each of these users belong to the same DB table of users, one type are male and one type are female. I intend on turning this into a request/response type method so that it returns a json object of the array matrix. I do not need any help with APIs or anything like that, I just need help with the core algorithm.

If you do not wish to provide an answer, please can you provide the name of the formula so I can research it? Or pseudocode, and I'll write it up myself. Many thanks.


Here is an attempt but it's not giving the expected outcomes and it is taking almost 2 seconds :O

Edit 2

I didn't realise it would be more complex than I thought, now I'm looking another output that would need to be valid (i.e. null values because sometimes we need to make it so that males or females don't have a match)

0=> [
"a"=> "k", "b"=> "l", "c"=> "m", "d"=> "n", "e"=> "o", "f"=> "p", "g"=> "q", "h"=> "r", "i"=> NULL, "j"=> NULL
1=> [
"a"=> "l","b"=> "m", "c"=> "n", "d"=> "o", "e"=> "p", "f"=> "q", "g"=> "r", "h"=> NULL, "i"=> NULL, "j"=> "k"

In this new output example the assume the key is male and the value is female. BUT it doesn't have to be, I'm merely just wanting a resultset I can use that has a male and a female match.

I have also tried as suggested: but same problem again it is taking too long, it takes average 1.5 seconds.

If we turned it into an API, it would have a massive impact if all the requests kept taking a few seconds all the time perhaps more since over the internet.

My Answer:
This algorithm does not have a specific name, but it can be considered a matching algorithm or a pairing algorithm. Here is a possible approach to achieve the desired output:

$males = ['A', 'B', 'C', 'D', 'E'];
$females = [1, 2, 3, 4, 5];

$matrix = [];

foreach ($males as $key => $male) {
$matrix[$key] = [];
foreach ($females as $index => $female) {
$matrix[$key][] = [$male, $female];

// To introduce null values for unmatched pairs
// Uncomment the following code
// $randomIndex = array_rand($females);
// unset($matrix[$randomIndex]);

echo json_encode($matrix);

This code snippet will generate the desired multidimensional array where each male is paired with a female based on their unique identifiers. You can also introduce null values for unmatched pairs by uncommenting the provided code.

Please note that the performance of this algorithm may vary depending on the size of the input arrays. If performance is a concern, you may need to optimize the algorithm further.

Rate this post

3 of 5 based on 8706 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us