Change the value of the array if it is repeated

I have an array that contains some articles of a website. The problem is that when the same article is in two different categories, it only shows 1. In this example, the categories are "Service" and "Sales".

If the article is duplicated and the category is "Sales", I want 1 of them to change to "Service" and vice versa.

Now the array I got (3 and 4 as duplicates and 7 and 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 execute (no more duplicates and 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 have 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 would be much appreciated.

+3


source to share


3 answers


Put your data in a variable $array

and try:



$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';

      

+3


source


If the order is not important to 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 a working demo .

+1


source


I have some code written here, 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);

      

0


source







All Articles