查找具有重复键值的元素并使用PHP添加新键

时间:2018-12-20 12:46:10

标签: php arrays

我有一个数组

$info    = array(
                [0] => array(
                            'id' => 1,
                            'uid' => '677674e21aed487fd7180da4a7619a9d'
                        ),
                [1] => array(
                            'id' => 1,
                            'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
                        ),
                [2] => array(
                            'id' => 1,
                            'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1'
                        ),
                [3] => array(
                            'id' => 1,
                            'uid' => '658284e5395a29bf34d21f30a854e965'
                        ),
                [4] => array(
                            'id' => 1,
                            'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
                        ),
                [5] => array(
                            'id' => 1,
                            'uid' => '677674e21aed487fd7180da4a7619a9d'
                        )                                       
)

如您所见,第0个索引和第5个索引的uid相同。同样,第二索引和第三索引的uid相同。

我想要一个PHP脚本,通过它我可以为重复的uid随机创建一个十六进制颜色代码。像这样说。

$info    = array(
                [0] => array(
                            'id' => 1,
                            'uid' => '677674e21aed487fd7180da4a7619a9d',
                            'col' => 'black'
                        ),
                [1] => array(
                            'id' => 1,
                            'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
                            'col' => 'green'
                        ),
                [2] => array(
                            'id' => 1,
                            'uid' => 'd3c98a10fe4e42fb1fe868008c0f4cc1',
                            'col' => 'green'
                        ),
                [3] => array(
                            'id' => 1,
                            'uid' => '658284e5395a29bf34d21f30a854e965'
                        ),
                [4] => array(
                            'id' => 1,
                            'uid' => '01f33ae45a463e0c1de4ad989b3ccad5'
                        ),
                [5] => array(
                            'id' => 1,
                            'uid' => '677674e21aed487fd7180da4a7619a9d',
                            'col' => 'black'
                        )                                       
)

如何用最短的执行时间来做到这一点?

1 个答案:

答案 0 :(得分:0)

进行此锻炼的方法可能多种多样,但是由于缺乏适当的响应,我想出了这个可能更长的代码。我在这里将答案发布给可能需要此答案的人。

$uidArray           = array(); // creating a blank array to feed each uid
$uidDuplicateArray  = array(); // creating a blank array as container to hold duplicate uid(s) only
foreach($all_data as $key => $ad)                                                               
{
    // iterate through each item of the list
    /.................
    .................. //
    $uidArray[]     = $ad['uid'];
}
foreach(array_count_values($uidArray) as $val => $c)
{
    if($c > 1) 
    {
        // if count value is more than 1, then its duplicate
        // set the uid duplicate array with key as uid and unique color code as value
        $uidDuplicateArray[$val] = sprintf('#%06X', mt_rand(0, 0xFFFFFF));
    }
}
foreach($all_data as $keyAgain => $adg)
{
    // iterating through each item of original data
    if(isset($uidDuplicateArray[$adg['uid']]))
    {
        // if the uid is key of the duplicate array, feed the value to original array in a new key.
        $all_data[$keyAgain]['color'] = $uidDuplicateArray[$adg['uid']];
    }

}

与每个LOC相关的每个注释都是不言自明的。

我想要这样做的原因是要在UI中将重复项标记为:-

enter image description here