Morshedul Arefin
Morshedul Arefin

Reputation: 1538

Merging two arrays removing duplicate keys from one

I have two arrays. Those are below:

First Array:

$arr1[0] = 'Programs';
$arr1[1] = 'Levels';
$arr1[2] = 'Presenters';
$arr1[3] = 'Levels';

Second Array:

$arr2[0] = 'Art';
$arr2[1] = 'Primary';
$arr2[2] = 'Kristine Ballard';
$arr2[3] = 'Secondary';

I want to get output like this:

Programs = Art
Levels = Primary, Secondary
Presenters = Kristine Ballard

Can anyone help?

Upvotes: 1

Views: 68

Answers (3)

Morshedul Arefin
Morshedul Arefin

Reputation: 1538

I got another solution by myself and this is working. I have just posted it below for the help to others who will see this thread. Here is my code:

$arr1[0] = 'Programs';
$arr1[1] = 'Levels';
$arr1[2] = 'Presenters';
$arr1[3] = 'Levels';

$arr2[0] = 'Art';
$arr2[1] = 'Primary';
$arr2[2] = 'Kristine Ballard';
$arr2[3] = 'Secondary';

$new_arr1 = array_values(array_unique($arr1));

for ($i = 0; $i < count($new_arr1); $i++)
{
    $var = 0;
    echo $new_arr1[$i]. ': ';
    for ($j = 0; $j < count($arr1); $j++)
    {
        if($new_arr1[$i] == $arr1[$j])
        {
            if($var == 0)
            {
                echo $arr2[$j];
            }
            else
            {
                echo ', ' . $arr2[$j];
            }
            $var++;
        }
    }
    echo '<br>';

}

Upvotes: 0

Marc
Marc

Reputation: 3709

With the following, you would create a multidimensional array, if there are multiple values of a key:

$arr1[0] = 'Programs';
$arr1[1] = 'Levels';
$arr1[2] = 'Presenters';
$arr1[3] = 'Levels';

$arr2[0] = 'Art';
$arr2[1] = 'Primary';
$arr2[2] = 'Kristine Ballard';
$arr2[3] = 'Secondary';

$newArray = array();

foreach($arr1 as $index => $key) {
    $newArray[$key][] = $arr2[$index];
}

print_r($newArray);

Output:

Array
(
    [Programs] => Array
        (
            [0] => Art
        )

    [Levels] => Array
        (
            [0] => Primary
            [1] => Secondary
        )

    [Presenters] => Array
        (
            [0] => Kristine Ballard
        )

)

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212412

$arr1[0] = 'Programs';
$arr1[1] = 'Levels';
$arr1[2] = 'Presenters';
$arr1[3] = 'Levels';

$arr2[0] = 'Art';
$arr2[1] = 'Primary';
$arr2[2] = 'Kristine Ballard';
$arr2[3] = 'Secondary';


$newArray = [];
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($arr1));
$mi->attachIterator(new ArrayIterator($arr2));
foreach($mi as list($key, $value)) {
    $newArray[$key][] = $value;
}
var_dump($newArray);

Note that the use of foreach() with list() requires PHP>=5.5.0

For earlier versions of PHP, the following will work:

$newArray = [];
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($arr1), 'key');
$mi->attachIterator(new ArrayIterator($arr2), 'value');
foreach($mi as $details) {
    extract($details);    
    $newArray[$key][] = $value;
}
var_dump($newArray);

Upvotes: 0

Related Questions