1

I have 2 arrays:

a1 = 1,2,3,4,5,6,7,8
a2 = 1,3,5,7

I want to be able to compare both arrays where they match, combine them without duplicates and then attach a prefix or letter to the matching values.

expected result:

a3 - match1,2,match3,4,match5,6,match7,8

I have looked at array_intersect(), but I'm not sure how I would use it in my example.

executable
  • 3,365
  • 6
  • 24
  • 52

2 Answers2

1

This could be one for https://adventofcode.com !

This is probably not the most efficient solution performance- or memory-wise, but it should do the trick.

$a1 = [1,2,3,4,5];
$a2 = [1,3,5];
$result = [];

foreach($a1 as $item) {
    if (in_array($item, $a2)) {
        $result[] = 'match' . $item;   
    } else {
        $result[] = $item;
    }
}

See it in action here: https://3v4l.org/PlTIZ

Loek
  • 4,037
  • 19
  • 35
  • Perfect, this is exactly what i was looking for thanks! – Murderous Koala Dec 21 '18 at 14:20
  • No problem! Please tick as an answer so other people can benefit from it too. – Loek Dec 21 '18 at 14:23
  • 1
    Good self-explanatory solution, however, make use of [pre-built PHP functions](http://php.net/manual/en/ref.array.php). No need to loop through arrays all the time, sure you'll find some good functions to replace foreach loops in the manual for future reference. – Jaquarh Dec 21 '18 at 15:20
  • @Jaquarh I did this on purpose, as I both find it easier to read and the `array` functions are a lot slower than `foreach`: https://stackoverflow.com/questions/18144782/performance-of-foreach-array-map-with-lambda-and-array-map-with-static-function – Loek Dec 22 '18 at 16:13
  • 1
    That is because `array_map` in this case, which your referring to, replaces the array with new values each time, hence `return`. Building a new array and pushing values in within a loop, in this case **will** be slower. In the case of the question you linked, `array_map` is not needed since the value **does not** change, thus `foreach` will have better performance. My comment wasn't meant to offend you, only, for future reference, point you in the direction of not **always** using `foreach`. – Jaquarh Dec 22 '18 at 18:11
  • I certaibly wasn't offended! Actually some very good advice and tips, thanks! I can certainly put this to good use – Loek Dec 23 '18 at 20:56
1

Alternatively, You can use array_map() to achieve this with better performance and maintainable code.

Live demo.

$arr_one = [1,2,3,4,5];
$arr_two = [1,3,5];

$new_arr = array_map(function($arg) use ($arr_two) {
    return in_array($arg, $arr_two) ? "prefix_{$arg}" : $arg;
}, $arr_one);

Output:

array(5) {
  [0]=>
  string(8) "prefix_1"
  [1]=>
  int(2)
  [2]=>
  string(8) "prefix_3"
  [3]=>
  int(4)
  [4]=>
  string(8) "prefix_5"
}

References

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Please note that the performance is *not* better with the array functions (often it's actually a lot worse), and the maintainability is pretty subjective. I needed to read your code multiple times before I got what was up. Even though, good answer and good alternative. – Loek Dec 22 '18 at 16:16
  • 1
    In this case, `array_map()` has achieved better performance. And in multiple other cases, like [this](https://3v4l.org/CAGEE) proves my point about utilising the tools PHP already has before looping through everything @Loek – Jaquarh Dec 22 '18 at 18:06