tags:

views:

61

answers:

5

I have an array that has 22 items in it (it may also have 7, 10 or 11 items in it)

Old, crappy example: $anArray = array(1=>'A',2=>'B',3=>'C',4=>'D',5=>'E',6=>'F',7=>'G',8=>'H',9=>'I');

Better example:

Array(
[25] => #
[50] => #
[53] => #
[55] => #
[56] => #
[57] => #
[58] => #
[59] => #
[60] => #
[61] => #
[62] => #
[63] => #
[64] => #
[65] => #
[66] => #
[67] => #
[68] => #
[69] => #
[70] => #
[71] => #
[72] => #
[73] => #)

I also have three variables:

$A='A'; $B='B'; $C='C';

I need change the first 1/3(*ish) of the items in the array from what they are to $A ...the 2nd third to $B ...and the final third to $C

Old, crappy example:Array([1]=>A [2]=>A [3]=>A [4]=>B [5]=>B [6]=>B [7]=>C [8]=>C [9]=>C)

Better example:

Array(
[25] => A
[50] => A
[53] => A
[55] => A
[56] => A
[57] => A
[58] => A
[59] => B
[60] => B
[61] => B
[62] => B
[63] => B
[64] => B
[65] => B
[66] => B
[67] => C
[68] => C
[69] => C
[70] => C
[71] => C
[72] => C
[73] => C)

Is there an easy and straight-forward way of doing this? Right now I'm doing lots of foreach with counters, etc... and it seems like to much work.

Also...The keys cannot change...sorry.

*By ish I mean about a third - 7-8-7 or 7-7-8 would work fine

A: 
array_replace($an_array,array_merge(
  array_pad($a,count($an_array)/3),
  array_pad($b,count($an_array)/3),
  array_pad($c,count($an_array)/3)
));

A bit contorted, and probably won't work like you want it to if count($an_array) is not divisible by 3, but probably better than the for equivalent.

Stefan Mai
+1  A: 

This must be for school because I can't see this having any logical use as a real function.

Anyways, an easier way to do it is to calculate the length of the array and then calculate which variable needs to be used for that particular position using a single for loop. There's no need to replace values if every single value will be changed, that's a waste of time.

$vals = array("A", "B", "C");
for ($i = 0; $i < count($array); $i++) {
    $newarray[$i] = $vals[floor($i/3)];
}

Edit: Sorry previous was just for 9... I'm sure you can figure it out for different lengths though.

animuson
Not for school at all...I have a grid I'm building that needs to be color coded in thirds based on score values from an employee review. Your option works but the keys in the array change and if it's not divisible by 3 it does not seem to work.
Jason
A: 

You might be interested in array_chunk

macek
+1  A: 
$c = intval(count($ary) / 3);

$result = 
    array_fill(1, $c, 'A') + 
    array_fill($c + 1, $c, 'B') + 
    array_fill($c * 2 + 1, $c, 'C');

ok, with the keys preserved

$ary = array(11=>'A',22=>'B',33=>'C',44=>'D',55=>'E',66=>'F',77=>'G',88=>'H',99=>'I',123=>'x',456=>'y',); 

$cc = count($ary);
$c1 = intval($cc / 3);
$c2 = intval(($cc - $c1) / 2);
$c3 = $cc - ($c1 + $c2);

$result = array_combine(
    array_keys($ary),
    array_fill(0, $c1, 'A') + 
    array_fill($c1, $c2, 'B') + 
    array_fill($c1 + $c2, $c3, 'C'));

print_r($result);
stereofrog
Looks good but that causes the key to change. Sorry that was not part of the original question.
Jason
@Jason, if you want keys to start from 1, just add 1 to each param (see update).
stereofrog
Thanks...unfortunately my example was a bit hasty. The keys could be a jumble of values. They don't start at 1.
Jason
@Jason see update
stereofrog
Awesome...thanks for the update
Jason
A: 

Maybe, something like this:

<?php

$array = array(
'2'=>'',
'3'=>'',
'5'=>'',
'8'=>'',
'9'=>'',
'13'=>'',
'55'=>'',
'24'=>'',
'16'=>'',
'37'=>'',
'86'=>'',
'45'=>'',
'32'=>'',
'35'=>'',
'37'=>'',
'26'=>'',
'29'=>'',
'87'=>'',
'68'=>'',
'69'=>'',
'60'=>'',
'65'=>'',
'43'=>'',
'23'=>'',
);

$aPart = array(1=>'A',2=>'B',3=>'C');
$thispart = 1;

$total = count($array);
$part = round($total / 3);

$count = 0;
foreach($array as $idx => $val){
    $count++;
    if($count >= $part){
        $thispart++;
        $count=0;
    }
    $array[$idx] = $aPart[$thispart];
}

print_r($array);

My print_r() output:

Array
(
    [2] => A
    [3] => A
    [5] => A
    [8] => A
    [9] => A
    [13] => A
    [55] => A
    [24] => B
    [16] => B
    [37] => B
    [86] => B
    [45] => B
    [32] => B
    [35] => B
    [26] => B
    [29] => C
    [87] => C
    [68] => C
    [69] => C
    [60] => C
    [65] => C
    [43] => C
    [23] => C
)
silent