0

I have two multidimensional arrays which I want to intersect using PHP array_intersect function, but prior to do the output for this it must also make a replacement for only one element of array.

Example:

$array1 = Array(
Array(37768201, 273, Array(602, 603, 604, 605, 606), 0),
Array(37483503, 473, Array(604, 605, 606), 0),
Array(37500944, 332, Array(602, 603), 0)
);

$array2 = Array(
Array(37768201, 273, Array(602, 603, 604, 605, 606), 13852),
Array(37483503, 473, Array(603, 604, 605, 606), 13853),
Array(37500944, 332, Array(602, 603, 604, 605), 13854),
Array(37483903, 152, Array(602, 603, 604, 605, 606), 13855),
Array(37483703, 175, Array(602, 603, 604, 605, 606), 13856)
);

array_intersect result:

$array1 = Array(
Array(37768201, 273, Array(602, 603, 604, 605, 606), 0),
Array(37483503, 473, Array(604, 605, 606), 0),
Array(37500944, 332, Array(602, 603), 0)
);

Expected result:

$array1 = Array(
Array(37768201, 273, Array(602, 603, 604, 605, 606), 13852),
Array(37483503, 473, Array(604, 605, 606), 13853),
Array(37500944, 332, Array(602, 603), 13854)
);

Could this be done using array_uintersect istead?

jthill
  • 55,082
  • 5
  • 77
  • 137

1 Answers1

0

You can add a little piece of code after calling array_intersect()

For your example:

foreach ($array1 as $key => $values)
  $array1[$key][3] = 13852;

Although if you said which data is variable, it'd be much easier. For example, the number 13852 seems variable in each execution and (maybe) in each sub array ($array2[0][3] != $array2[1][3]). Then that code would be invalid and you should use this instead:

foreach ($array1 as $key => $values)
  $array1[$key][3] = $array2[$key][3];

And there are just many more possible cases.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
  • 1
    Thank you! This came like a pill after a small headache. ;) – Claudiu Olaru Apr 07 '13 at 22:47
  • There still might be a problem though when the order of elements from those 2 big arrays is different. – Claudiu Olaru Apr 18 '13 at 18:49
  • @ClaudiuOlaru , I don't understand why you unaccepted my answer. That's just one of the problems that might arise, but there are hundreds more, as I stated: `And there are just many more possible cases.`. My answer properly answered your question 2 weeks ago, if your case **now** is different, you should try to solve it yourself, then, in case you cannot, ask a new question here or pay a developer. Or use some big work-with-all-cases code to merge arrays (but don't expect anyone to do something like that here for free). – Francisco Presencia Apr 22 '13 at 15:52