/    Sign up×
Community /Pin to ProfileBookmark

Assign Number an array value

I’ve got a list of ID’s coming from a DB table in a particular order. They’ll have a ”groupId’ value.

I need to assign each one a “color” from an array (RGB value). For example my data would be

12
3
45
12
12
3
45
3
45

So for example, so the first one is 12, assign it the first color in my array, next is 3 so gets the second value. When I come back to 12, I want to reuse that one.

My approach has been to pass in the ID to a function, see if it’s in an array. If so return it. If not add it and return the value.

I’m a bit lost however

“`

function colorMap($groupId){

$colorAssignment = array();
if (in_array($colorAssignment, $groupId)){
return $colorAssignment[???]];
} else {
$colorAssignment[] = array($groupId, ??);
}

}

“`

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 10.2021 — I recommed to use the group ID as a key in the color assignment:
``<i>
</i> $rgbValues = [12, 3, 45, // and so on
$idx = 0;
$colorAssignment = [];
function colorMap($groupId, &amp;$idx){

if (!isset($colorAssignment[$groupId])){
$colorAssignment[$groupId] = $rgbValues[$idx];
$idx++;
if ($idx &gt;= count($rgbValues)) $idx = 0;
}
return $colorAssignment[$groupId];

}<i>
</i>
``

(not tested)
Copy linkTweet thisAlerts:
@SempervivumMay 10.2021 — BTW: Single backticks (probably created by the button `&lt;/&gt;</C>) won't work reliably when posting code. You better use code tags: <C>your code here` or triple backticks.

I edited your posting accordingly.
Copy linkTweet thisAlerts:
@kiwisauthorMay 10.2021 — Notice: Undefined variable: rgbValues

Notice: Undefined variable: idx in

Notice: Trying to access array offset on value of type null in

Can I access these from inside my function?

``<i>
</i>$rgbValues = ['#cbe3f7', '#ecff96', '#e3b8fc', '#cbf7d2', '#ffd1d1', '#f0dab1'];
$idx = 1;
$colorAssignment = [];
function colorMap($groupId){
if (isset($colorAssignment[$groupId])){
return $colorAssignment[$groupId];
} else {
$colorAssignment[$groupId] = $rgbValues[$idx];
$idx++;
//if ($idx &gt;= count($rgbValues)) $idx = 0;
$v = $rgbValues[$idx];
return $rgbValues[$idx];
}

}
echo colorMap(44);<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumMay 10.2021 — Sorry, I overlooked these. You can hand them over as parameters (references):
```
$rgbValues = ['#cbe3f7', '#ecff96', '#e3b8fc', '#cbf7d2', '#ffd1d1', '#f0dab1'];
$idx = 1;
$colorAssignment = [];
function colorMap($groupId, $rgbValues, &$idx, &$colorAssignment){
if (isset($colorAssignment[$groupId])){
return $colorAssignment[$groupId];
} else {
$colorAssignment[$groupId] = $rgbValues[$idx];
$idx++;
//if ($idx >= count($rgbValues)) $idx = 0;
$v = $rgbValues[$idx];
return $rgbValues[$idx];
}


}
echo colorMap(44, $rgbValues, $idx, $colorAssignment);
Copy linkTweet thisAlerts:
@kiwisauthorMay 10.2021 — Yeah but when I now do this,

``<i>
</i>$rgbValues = ['#cbe3f7', '#ecff96', '#e3b8fc', '#cbf7d2', '#ffd1d1', '#f0dab1'];
$idx = 1;
$colorAssignment = [];
function colorMap($groupId, $rgbValues, &amp;$idx, &amp;$colorAssignment){
if (isset($colorAssignment[$groupId])){
return $colorAssignment[$groupId];
} else {
$colorAssignment[$groupId] = $rgbValues[$idx];
$idx++;
//if ($idx &gt;= count($rgbValues)) $idx = 0;
$v = $rgbValues[$idx];
return $rgbValues[$idx];
}


}
echo colorMap(44, $rgbValues, $idx, $colorAssignment) ."&lt;BR&gt;";
echo colorMap(33, $rgbValues, $idx, $colorAssignment) ."&lt;BR&gt;";
echo colorMap(12, $rgbValues, $idx, $colorAssignment) ."&lt;BR&gt;";
echo colorMap(44, $rgbValues, $idx, $colorAssignment) ."&lt;BR&gt;";<i>
</i>
``


output 1 and 4 are not the same
Copy linkTweet thisAlerts:
@SempervivumMay 10.2021 — The code in my first reply was better (and shorter), we just had to add the parameters:
``<i>
</i>$rgbValues = ['#cbe3f7', '#ecff96', '#e3b8fc', '#cbf7d2', '#ffd1d1', '#f0dab1'];
$idx = 1;
$colorAssignment = [];
function colorMap($groupId, $rgbValues, &amp;$idx, &amp;$colorAssignment)
{
if (!isset($colorAssignment[$groupId])) {
$colorAssignment[$groupId] = $rgbValues[$idx];
$idx++;
//if ($idx &gt;= count($rgbValues)) $idx = 0;
}
return $colorAssignment[$groupId];
}
echo colorMap(44, $rgbValues, $idx, $colorAssignment) . "&lt;BR&gt;";
echo colorMap(33, $rgbValues, $idx, $colorAssignment) . "&lt;BR&gt;";
echo colorMap(12, $rgbValues, $idx, $colorAssignment) . "&lt;BR&gt;";
echo colorMap(44, $rgbValues, $idx, $colorAssignment) . "&lt;BR&gt;";<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogMay 10.2021 — Challenge accepted: :) (EDIT: next step would be to encapsulate it in a function/method like Sempervivum did.)
[code=php]
$ids = [12, 3, 45, 9, 2, 12, 7, 55, 12, 62, 1, 3, 45, 3, 45];
$rgbValues = ['#cbe3f7', '#ecff96', '#e3b8fc', '#cbf7d2', '#ffd1d1', '#f0dab1'];
$keys = array_unique($ids);
$keyColors = [];
foreach($keys as $ix => $key) {
$keyColors[$key] = $rgbValues[$ix % count($rgbValues)];
}
var_export($keyColors);
foreach($ids as $id) {
echo "<li style='color:{$keyColors[$id]}'>$id</li>n";
}
[/code]

Output:
[code=text]
array (
12 => '#cbe3f7',
3 => '#ecff96',
45 => '#e3b8fc',
9 => '#cbf7d2',
2 => '#ffd1d1',
7 => '#cbe3f7',
55 => '#ecff96',
62 => '#cbf7d2',
1 => '#ffd1d1',
)

<li style='color:#cbe3f7'>12</li>
<li style='color:#ecff96'>3</li>
<li style='color:#e3b8fc'>45</li>
<li style='color:#cbf7d2'>9</li>
<li style='color:#ffd1d1'>2</li>
<li style='color:#cbe3f7'>12</li>
<li style='color:#cbe3f7'>7</li>
<li style='color:#ecff96'>55</li>
<li style='color:#cbe3f7'>12</li>
<li style='color:#cbf7d2'>62</li>
<li style='color:#ffd1d1'>1</li>
<li style='color:#ecff96'>3</li>
<li style='color:#e3b8fc'>45</li>
<li style='color:#ecff96'>3</li>
<li style='color:#e3b8fc'>45</li>
[/code]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...