dongnuo4594 2018-06-21 05:36
浏览 150
已采纳

如何在php数组中为特定键的相同值添加唯一标识符

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.

  • 写回答

1条回答 默认 最新

  • douwuying4709 2018-06-21 06:29
    关注

    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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算