简体   繁体   中英

How can i add a unique identifier to the same value of specific key in php array

I have an array

Array
(
    [0] => Array
        (
            [proomsize] => dasfdsfdsf
            [proomtype] => Bedroom
        )

    [1] => Array
        (
            [proomsize] => dasfdsfsd
            [proomtype] => Bedroom
        )

    [2] => Array
        (
            [proomsize] => dfadsf
            [proomtype] => KitchenDinner
        )   


    [4] => Array
        (
            [proomsize] => dafdsads
            [proomtype] => Reception
        )

    [5] => Array
        (
            [proomsize] => fdsafdsfdsf
            [proomtype] => Reception
        )

    [6] => Array
        (
            [proomsize] => adfadfdsf
            [proomtype] => Reception
        )
    )

Now i need is if there is more than one $array['proomtype'] of same value i want to append a number at the end in my foreach loop.. so when i loop through it i want a result like this

Bedroom 1 : dasfdsfdsf
Bedroom 2 : dasfdsfsd
KitchenDinner : dfadsf
Reception 1 : dasfdsfsd
Reception 2 : adfdsf
Reception 3 : dfdfdf

how can i achieve that in a single loop i have tried using this

<?php 
$BedroomCounter = 0;
if ($value['proomtype'] == 'Bedroom') {
    if ($BedroomCounter == 0) {
       echo $value['proomtype'] ':' .$value['proomsize'] . '<br>'; 
    }else{
        echo $value['proomtype'] . $BedroomCounter . ':' .$value['proomsize'] . '<br>'; 
    }

    $BedroomCounter++;
}

the issue is i have repeat this for each n every roomtype.. which is not something i want to.

The way this works is by first using array_count_values() to count the number of each type of room, this helps know when we need to add a suffix or not.
It then creates a copy of this array with the current counter for each room type starting at 1 using array_fill_keys() .

The code then loops through all of the entries looking up the room type and if the room type has more than 1 entry it adds the suffix from the counter array and increments that counter.

$duplicates = array_count_values(array_column($value, "proomtype"));
$currentCount = array_fill_keys(array_keys($duplicates), 1);
foreach ( $value as $key=>$entry ){
    if ( $duplicates[$entry["proomtype"]] > 1 ) {
        $value[$key]["proomtype"] .= " ".$currentCount[$entry["proomtype"]];
        $currentCount[$entry["proomtype"]]++;
    }
}
print_r($value);

The output with your example data is...

Array
(
    [0] => Array
        (
            [proomsize] => dasfdsfdsf
            [proomtype] => Bedroom 1
        )

    [1] => Array
        (
            [proomsize] => dasfdsfsd
            [proomtype] => Bedroom 2
        )

    [2] => Array
        (
            [proomsize] => dfadsf
            [proomtype] => KitchenDinner
        )

    [4] => Array
        (
            [proomsize] => dafdsads
            [proomtype] => Reception 1
        )

    [5] => Array
        (
            [proomsize] => fdsafdsfdsf
            [proomtype] => Reception 2
        )

    [6] => Array
        (
            [proomsize] => adfadfdsf
            [proomtype] => Reception 3
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM