Remco A
Remco A

Reputation: 147

Change an array value if duplicate

I have an array which holds some articles of a website. The problem is: when the same article is in 2 different categories it only shows 1. In this example the categories are 'Service' and 'Sales'.

If the article is duplicate and the category is 'Sales' I want 1 of them to change to 'Service' and vice versa.

The array I got now(3 & 4 as duplicates and 7 & 8):

Array
(
    [0] => Array
        (
            [0] => Sales
            [1] => assistentiesystemen
            [2] => www.youtube.com/video/38BbjLmVJXk
            [3] => Park assist
        )


[1] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/3lGfTZdVK1s
        [3] => Multi Collision braking system
    )

[2] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/6mgDraWpGvE
        [3] => Area view
    )

[3] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/II68oVm4zro
        [3] => Lane Assist
    )

[4] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/II68oVm4zro
        [3] => Lane Assist
    )

[5] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/N0fa4dUBkvE
        [3] => Trailer assist
    )

[6] => Array
    (
        [0] => Service
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/NCNDyW6Yr1g
        [3] => Ruitenwissers
    )

[7] => Array
    (
        [0] => Service
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/PJEC-yqUwzE
        [3] => Bandenafdichtset
    )

[8] => Array
    (
        [0] => Service
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/PJEC-yqUwzE
        [3] => Bandenafdichtset
    )

)

The array I want to accomplish (No duplicates anymore and the values changed):

Array
(
    [0] => Array
        (
            [0] => Sales
            [1] => assistentiesystemen
            [2] => www.youtube.com/video/38BbjLmVJXk
            [3] => Park assist
        )


[1] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/3lGfTZdVK1s
        [3] => Multi Collision braking system
    )

[2] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/6mgDraWpGvE
        [3] => Area view
    )

[3] => Array
    (
        [0] => Service
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/II68oVm4zro
        [3] => Lane Assist
    )

[4] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/II68oVm4zro
        [3] => Lane Assist
    )

[5] => Array
    (
        [0] => Sales
        [1] => assistentiesystemen
        [2] => www.youtube.com/video/N0fa4dUBkvE
        [3] => Trailer assist
    )

[6] => Array
    (
        [0] => Service
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/NCNDyW6Yr1g
        [3] => Ruitenwissers
    )

[7] => Array
    (
        [0] => Sales
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/PJEC-yqUwzE
        [3] => Bandenafdichtset
    )

[8] => Array
    (
        [0] => Service
        [1] => veilig-op-de-weg
        [2] => www.youtube.com/video/PJEC-yqUwzE
        [3] => Bandenafdichtset
    )

)

What I tried:

$length = count ($sorted);

    for ($c = 0; $c < $length; $c++) {
        $check_array = $sorted[$c];

        for ($x = 0; $x < $length; $x++) {
            $compare_array = $sorted[$x];

            if ($x != $i){

                if($check_array[2] == $compare_array[2] && $check_array[0] == $compare_array[0]){
                    //print_r ($check_array);
                    //print_r ($compare_array);
                    if($check_array[0] == 'Sales'){
                        $compare_array[0] = 'Service';
                    }
                    if($check_array[0] == 'Service'){
                        $compare_array[0] = 'Sales';
                    }
                }

            }

        }
    }

Any help will be much appreciated.

Upvotes: 3

Views: 115

Answers (3)

MahdiY
MahdiY

Reputation: 1306

Put your data in $array variable and try it:

$hash = array_map( function ( $value ) { return md5( implode( $value ) ); } , $array);

$keys = array_keys( array_diff_assoc( $hash, array_unique( $hash ) ) );

foreach( $keys as $key )
    $array[ $key ][0] = $array[ $key ][0] == 'Service' ? 'Sales' : 'Service';

Upvotes: 3

sevavietl
sevavietl

Reputation: 3802

If the order is not important for you, you can use array_multisort, array_column and array_walk:

$replacements = [
    'Sales' => 'Service',
    'Service' => 'Sales'
];

// Sort array by youtube url.
array_multisort($array, array_column($array, 2));

array_walk($array, function (&$curr, $_, &$prev) use ($replacements) {
    if (
        $prev !== null
        && $prev[2] === $curr[2]
    ) {
        $curr[0] = $replacements[$curr[0]];
    }

    $prev = $curr;
}, null);

Here is working demo.

Upvotes: 1

Sohel Rana
Sohel Rana

Reputation: 607

Here I have a written some code, you can try it.

<?php    
$array = [
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/38BbjLmVJXk',
        3 => 'Park assist',
    ],
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/3lGfTZdVK1s',
        3 => 'Multi Collision braking system'
    ],
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/6mgDraWpGvE',
        3 => 'Area view'
    ],
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/II68oVm4zro',
        3 => 'Lane Assist'
    ],
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/II68oVm4zro',
        3 => 'Lane Assist'
    ],
    [
        0 => 'Sales',
        1 => 'assistentiesystemen',
        2 => 'www.youtube.com/video/N0fa4dUBkvE',
        3 => 'Trailer assist'
    ],
    [
        0 => 'Service',
        1 => 'veilig-op-de-weg',
        2 => 'www.youtube.com/video/NCNDyW6Yr1g',
        3 => 'Ruitenwissers'
    ],
    [
        0 => 'Service',
        1 => 'veilig-op-de-weg',
        2 => 'www.youtube.com/video/PJEC-yqUwzE',
        3 => 'Bandenafdichtset'
    ],
    [
        0 => 'Service',
        1 => 'veilig-op-de-weg',
        2 => 'www.youtube.com/video/PJEC-yqUwzE',
        3 => 'Bandenafdichtset'
    ]
];

/**
 * @param $firstIndexValue
 * @param $thirdIndexValue
 * @param $searchArray
 * @return bool
 */
function isExistValue($firstIndexValue, $thirdIndexValue, $searchArray)
{
    foreach ($searchArray as $element) {
        if ($firstIndexValue == $element[0] && $thirdIndexValue == $element[3]) {
            return true;
        }
    }

    return false;
}


$newArray = [];
foreach ($array as $arr) {
    $isExist = isExistValue($arr[0], $arr[3], $newArray);
    if ($isExist) {
        if ($arr[0] == 'Sales') {
            $arr[0] = 'Service';
        } elseif ($arr[0] == 'Service') {
            $arr[0] = 'Sales';
        }
    }
    $newArray[] = $arr;
}

var_dump($newArray);

Upvotes: 0

Related Questions