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

如何在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.

图片转代码服务由CSDN问答提供 功能建议

我有一个数组

  Array 
(
  [0] =&gt;数组
(
 [proomsize] =&gt; dasfdsfdsf 
 [proomtype] =&gt;卧室
)
 
 [1] =&gt;数组
(
 [proomsize]  =&gt; dasfdsfsd 
 [proomtype] =&gt;卧室
)
 
 [2] =&gt;数组
(
 [proomsize] =&gt; dfadsf 
 [proomtype] =&gt; KitchenDinner 
  )
 
 
 [4] =&gt;数组
(
 [proomsize] =&gt; dafdsads 
 [proomtype] =&gt;接收
)
 
 [5] =&gt;数组\  n(
 [proomsize] =&gt; fdsafdsfdsf 
 [proomtype] =&gt;接收
)
 
 [6] =&gt;数组
(
 [proomsize] =&gt; adfadfdsf 
 [proomtype]  ] =&gt;接收
)
)
   
 
 

现在我需要的是,如果我想要多个具有相同值的$ array ['proomtype'] 在最后添加一个数字 我的foreach循环..所以当我循环通过它我想要一个这样的结果

 卧室1:dasfdsfdsf 
Bedroom 2:dasfdsfsd 
KitchenDinner:dfadsf 
Reception 1:dasfdsfsd \  nReception 2:adfdsf 
Reception 3:dfdfdf 
   
 
 

我如何在一个循环中实现这一点我尝试过使用这个

 &lt;?php 
 $ BedroomCounter = 0; 
if($ value ['proomtype'] =='Bedroom'){
 if($ BedroomCounter == 0){
 echo $ value ['  proomtype']':'。$ value ['proomsize']。  '&LT峰; br&GT;';  
} else {
 echo $ value ['proomtype']。  $ BedroomCounter。  ':'。$ value ['proomsize']。  '&LT峰; br&GT;';  
 
 
 
 $ BedroomCounter ++; 
} 
   
 
 

问题是我为每个房间类型的每一个重复这个..这不是我想要的东西 到。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题