-2

I have three arrays as follows:

$a=Array ( [0] => 'member0' [1] => 'member1' [2] => 'member2' [3] => 'member1');
$b=Array ( [0] => 'id0' [1] => 'id1' [2] => 'id2' [3] => 'id1');
$c=Array ( [0] => 'tf0' [1] => 'tf1' [2] => 'tf2' [3] => 'tf1');

I would like to group their values according to their keys in a 2 dimensional array to output the following:

$2dim_array=array(array('member0','id0','tf0'),array('member1','id1','tf1'),array('member2','id2','tf2'),array('member1','id1','tf1'));

After, I would like to remove any duplicate array inside of the previous 2 dimensional array and output something like this:

$remove_duplicates=array(array('member0','id0','tf0'),array('member1','id1','tf1'),array('member2','id2','tf2'));

How can I do this?

Note: The arrays of this example only contain 3 elements each, but the length of my arrays can be variable (undefined number of keys).

ekad
  • 14,436
  • 26
  • 44
  • 46
Sarah
  • 325
  • 6
  • 17

2 Answers2

2

Use PHP's array_map() function

$a = ['member0','member1','member2','member1'];
$b = ['id0','id1','id2','id1'];
$c = ['tf0','tf1','tf2','tf1'];

// run three arrays in loop with array_map
$result = array_map(function($a1, $b1, $c1){
        return [ $a1, $b1, $c1];
}, $a, $b, $c);

// get unique records
$result = array_map('unserialize', array_unique(array_map('serialize', $result)));
echo '<pre>'; print_r($result);

Unique record link:-https://stackoverflow.com/a/2442315/4198099

output:-

Array
(
    [0] => Array
        (
            [0] => member0
            [1] => id0
            [2] => tf0
        )

    [1] => Array
        (
            [0] => member1
            [1] => id1
            [2] => tf1
        )

    [2] => Array
        (
            [0] => member2
            [1] => id2
            [2] => tf2
        )

)

Hope it will help you :-)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
1

You can use array_map() for group according to keys:

$a = array('member0','member1','member2','member1');
$b = array('id0','id1','id2','id1');
$c = array('tf0','tf1','tf2','tf1');

$merge = array_map(function($a1, $b1, $c1){
    return array($a1, $b1, $c1);
}, $a, $b, $c);

For removing duplicate records you can use array_map() and array_unique():

// remove duplicates by using array_map() and `array_unique()` for multidimensional array
$duplicateRemoved = array_map("unserialize", array_unique(array_map("serialize", $merge)));

// rearrange the array
$rearrangeArray = array_values($duplicateRemoved);

echo "<pre>";
print_r($rearrangeArray);

Result:

Array
(
    [0] => Array
        (
            [0] => member0
            [1] => id0
            [2] => tf0
        )

    [1] => Array
        (
            [0] => member1
            [1] => id1
            [2] => tf1
        )

    [2] => Array
        (
            [0] => member2
            [1] => id2
            [2] => tf2
        )

)
devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    upvoted for your rearrange index but I still not sure its necessity but I know if you have added it then there must be a reason ;-) – Ravi Hirani Mar 10 '16 at 11:03