3

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.

4
  • What are those few things ?? can you show ew things you did Commented Mar 22, 2017 at 9:58
  • Added my code which doesn't work Commented Mar 22, 2017 at 10:02
  • you are trying to change the sales to service and service to sales Commented Mar 22, 2017 at 10:05
  • Thats right, only if the youtube url and the category are the same. Commented Mar 22, 2017 at 10:09

3 Answers 3

3

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';
Sign up to request clarification or add additional context in comments.

10 Comments

nice approach. but I think you can just use $hash = array_map('serialize', $array); for "hashing".
Only the sales duplicates are handled right, it doesnt seem to work on the service duplicates. Any ideas?
7 & 8 is service duplicates. it's right! order is important for you?
7 & 8 are indeed service duplicates. They both still are after running them code. While 1 of the needs to be sales.
It works on my array example above, but when I implement it for my real array with 113 elements the service ones don't work. Any idea?
|
1

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.

1 Comment

Works, thanks! I think MahdiY's answer is the best solution for me.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.