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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)