0

I have an array with products where i want to search for duplicate names and increment the duplicate values.
I tried to create an empty array and push all the title values key[0] to it, then i found how many duplicates i have for each value of key[0] with array_count_values.
This is my array;

$products=Array
(
    [0] => Array
        (
            [0] => 'Intel I3',
            [1] => 146,
            [2] => 'intel-i3'
        ),

    [1] => Array
        (
            [0] => 'Intel I3',
            [1] => 146,
            [2] => 'intel-i3'
        ),
    [2] => Array
        (
            [0] => 'Intel I3',
            [1] => 250,
            [2] => 'intel-i3'
        ),
    [3] => Array
        (
            [0] => 'AMD graphic',
            [1] => 146,
            [2] => 'amd-graphic'
        )
);

I want this result with incrementing by 1.

How can i get this result ?

$products=Array
(
    [0] => Array
        (
            [0] => 'Intel I3',
            [1] => 146,
            [2] => 'intel-i3'
        ),

    [1] => Array
        (
            [0] => 'Intel I3_1',
            [1] => 146,
            [2] => 'intel-i3'
        ),
    [2] => Array
        (
            [0] => 'Intel I3_2',
            [1] => 250,
            [2] => 'intel-i3'
        ),
    [3] => Array
        (
            [0] => 'AMD graphic',
            [1] => 146,
            [2] => 'amd-graphic'
        )
);
4
  • [0] => 'Intel I3_1', is the difference Commented Dec 11, 2014 at 14:32
  • I also didn't at first: Look at the Intels. There is _1 and _2 behind the name Commented Dec 11, 2014 at 14:32
  • the difference is on array key [0] of every array intel i3, intel i3_1, intel i3_2 Commented Dec 11, 2014 at 14:33
  • Do not you need count? I see, you need serial number of duplicate. Commented Dec 11, 2014 at 14:34

3 Answers 3

3
$titleCounts = array();

foreach ($products as &$product) {

    if (!isset($titleCounts[$product[0]])) {
        $titleCounts[$product[0]] = 0;
    } else {
        $titleCounts[$product[0]]++;
        $product[0] = ($product[0].'_'.$titleCounts[$product[0]]);
    }
}

You can see this work here:

http://ideone.com/4xoGgP

Sign up to request clarification or add additional context in comments.

1 Comment

this is a great example
0
<?php
$counter = [];
$i = 0;
foreach($products as $product){
    $counter[] = $product[2];
    $vals = array_count_values($counter);
    $current_number = $vals[$product[2]];
    if($current_number)
        $products[$i][0] .= '_'.$current_number;
    $i++;
}

That should do.

1 Comment

Add a $ sign in the foreach to product.
0

Tested, and it works.

$tmpArray = array();

foreach ($products as &$product) {
    $name = $product[0];
    if (array_key_exists($name, $tmpArray)) {
        $product[0] = $name . '_' . $tmpArray[$name];
        $tmpArray[$name]++;
    } else {
        $tmpArray[$name] = 1;
    }
}
unset($product);

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.