-2

I have an array

$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'
                        )                                       
)

As you can see, uid of 0th index and 5th index are same. Similarly, uid of 2nd index and 3rd index are same.

I want a PHP script by which I can randomly create one hexadecimal color code for duplicate uids. Say something like this.

$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'
                        )                                       
)

How can I do this with the most minimum execution time?

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • 3
    I'm pretty sure with that repo you really should know how to ask questions on StackOverflow. Could you show a code of example you've tried? – AnTrakS Dec 20 '18 at 12:48
  • you could use `array_count_values()` and push into the array if return > 1 – treyBake Dec 20 '18 at 12:48
  • Do you have any code already or are you looking for others to do the job for you? – Aleks G Dec 20 '18 at 12:48
  • 1
    Fastest way is to extract `uid`s, making them keys of another array, then iterate over first array and add color if `uid`s is met more than once. This will take 2 walks over source array. – u_mulder Dec 20 '18 at 12:50

1 Answers1

0

There might be various ways for doing this workout, but due to lack of proper response, I came up with this probable lengthier code. I am posting the answer here for people who might need this.

$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']];
    }

}

Each comment associated with each LOC is self explanatory.

The reason I wanted this, is to mark the duplicates in UI like this:-

enter image description here

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • It wasn't that I didn't want to help you, it is one of my principles that I don't post answers to questions that lack a coding attempt. – mickmackusa Dec 20 '18 at 14:17
  • The question asked for "minimum execution time". Why should I post a code which takes the longest time? – Saswat Dec 20 '18 at 14:34
  • It would be a starting point. Is "minimum execution time" some sort of excuse for not posting a coding attempt? – mickmackusa Dec 20 '18 at 14:35
  • Since I can put the code anyway, there was no need to give an excuse, especially when I am an user with such repo (as you yourself mentioned). – Saswat Dec 20 '18 at 16:48
  • I didn't mention repo, but yes, I agree with the others that for someone who has been here for almost 7 years and has thousands of rep points, It is shocking that you didn't post a complete question. – mickmackusa Dec 20 '18 at 19:59