0

hello,
I have four arrays.
Three arrays are in one group and fourth is in the second group. And I need to use array_diff somehow between theese two groups.
First group has one array which contains unique numbers (like barcode number), second array with a product name and third array with weight data.
Theese three arrays depend on each other with index number.
Second group has only one array which contains unique numbers (barcode).
When I use array_diff function in php between two barcode arrays (barcode array in first group and barcode array in second group), how can I apply this difference on second and third array?

Data in first group:
First array has:
[0] => 4711
[1] => 4712
[2] => 4713 *here is difference
[3] => 4714

Second array has:
[0] => ALFA ROMEO
[1] => BMW
[2] => SKODA
[3] => TOYOTA

Third array has:
[0] => 6,5
[1] => 65
[2] => 0,2
[3] => 2,9

Data in second group:
Array has:
[0] => 4711
[1] => 4712
[2] => 2584 *here is difference
[3] => 4714

I whish that I could get the following arrays (using array_diff) which contains:
1st array:
[0] => 4713
2nd array:
[0] => SKODA
3rd array:
[0] => 0,2

Thank you in advance :)

2
Contributors
2
Replies
23
Views
1 Year
Discussion Span
Last Post by bprosic
1

Hi,

array_diff() will return the index key, so you can get the corresponding values for each array, as example:

<?php

$a = [4711, 4712, 4713, 4714];
$b = ['ALFA ROMEO', 'BMW', 'SKODA', 'TOYOTA'];
$c = [6.5, 65, 0.2, 2.9];

$d = [4711, 4712, 2856, 4714];

$diff = array_diff($a, $d);
$key  = key($diff);

$template = '%s %s %s';
echo sprintf($template, $a[$key], $b[$key], $c[$key]);

Note that if array_diff() returns more results you will have to loop, as the internal pointer will return only the first index key.

Edited by cereal

1

From your idea, this came out ;)

$diff = array_diff($a, $d);
foreach ($diff as $key => $value) {
    $a[]=$a0[$key];
     $b[]=$b0[$key];
     $c[]=$c0[$key];
}

Thank you mr. Cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.