Page 1 of 1

Tricky array problem

Posted: Fri Nov 21, 2003 10:14 am
by JayBird
i have two arrays containing the following values

Array1

Code: Select all

Array
(
    [0] => 250
    [1] => 251
    [2] => 252
    [3] => 253
    [4] => 254
    [5] => 257
    [6] => 258
    [7] => 264
    [8] => 265
    [9] => 266
    [10] => 278
    [11] => 279
    [12] => 280
    [13] => 281
    [14] => 282
    [15] => 271
    [16] => 272
    [17] => 273
    [18] => 274
    [19] => 148
    [20] => 149
    [21] => 152
    [22] => 153
    [23] => 154
    [24] => 34
    [25] => 35
    [26] => 36
    [27] => 37
    [28] => 40
    [29] => 41
    [30] => 42
    [31] => 43
    [32] => 44
    [33] => 47
    [34] => 48
    [35] => 49
    [36] => 218
    [37] => 219
    [38] => 222
    [39] => 127
    [40] => 128
    [41] => 131
    [42] => 132
    [43] => 131
)
Array2

Code: Select all

Array
(
    [0] => Mark
    [1] => Mark
    [2] => Mark
    [3] => Mark
    [4] => Mark
    [5] => Mark
    [6] => Mark
    [7] => Katie
    [8] => Katie
    [9] => Katie
    [10] => Sean
    [11] => Sean
    [12] => Sean
    [13] => Sean
    [14] => Sean
    [15] => Claire
    [16] => Claire
    [17] => Claire
    [18] => Claire
    [19] => Alison
    [20] => Alison
    [21] => Alison
    [22] => Alison
    [23] => Alison
    [24] => Mark
    [25] => Mark
    [26] => Mark
    [27] => Mark
    [28] => Mark
    [29] => Mark
    [30] => Mark
    [31] => Mark
    [32] => Mark
    [33] => Mark
    [34] => Mark
    [35] => Mark
    [36] => Katie
    [37] => Katie
    [38] => Katie
    [39] => Rebecca
    [40] => Rebecca
    [41] => Rebecca
    [42] => Rebecca
    [43] => Mark
)
The first array is the day number of the year that someone is having as holiday from work, the second array corresponds to that persons name.

On my calendar script, on each day, i check to see if the current day is present in Array1. If it is, i output the employees name into that days cell.

The problem come when two people have the same day off.

array_search only returns the last one it finds (or the first one, not sure).

My ideal solution would be to create new arrays. The first would contain the unique days that people are having off. If on one of those days, there are two people off, the corresponding value in athe other array would be something like "Mark, Claire".

How can i create these two new arrays from the ones i currently have. Arrays really aren't my strong point, so any help would be greatly appreciated.

Thanks

Mark

Posted: Fri Nov 21, 2003 11:09 am
by gjb79
Where do you keep the arrays? do you directly add a new item to the array in code form? load it from a text or some other file? or use a sql database to store the names and information? There might be a better way to condence the arrays so you do not have repeating data, thus making it smaller, more efficient, more productive.

Posted: Fri Nov 21, 2003 11:14 am
by JayBird
well, if you wanna suggest a better way from the start heres the situation.

the MySQL Table

Code: Select all

Field       Type        Null    Key     Default  Extra         
----------  ----------  ------  ------  -------  --------------
id          tinyint(4)          PRI     (NULL)   auto_increment
who         text        YES             (NULL)                 
from_date   date      YES             (NULL)                 
to_date     date       YES             (NULL)
from here i am currently working out every single day that every person has off.

Any better ideas?

Mark

Posted: Fri Nov 21, 2003 11:55 am
by Weirdan
First of all, you can use multidimension array to arrange you data, e.g.:

Code: Select all

Array(2) {
  ї131]=>Array(2){
       ї0]=>"Rebecca"
       ї1]=>"Mark"
  }
...............
}
To solve your particular problem use the following function:

Code: Select all

/** array_multisearch - searches the array for multiple elements.
@return array
@param $haystack array Array to search in
@param $needle mixed What we are searching for
@param $strict bool use === instead of ==
*/
function array_multisearch($needle,$haystack,$strict=false){
   $res=array();
   foreach($haystack as $elt){
     if($strict){
         if($needle===$elt) $res[]=$elt;
     }else{
         if($needle==$elt) $res[]=$elt;
     }
   }
   return $res;
}

Posted: Fri Nov 21, 2003 12:55 pm
by JayBird
i can't fint array_multisearch in the PHP manual!?

Posted: Fri Nov 21, 2003 1:19 pm
by mrvanjohnson
Bech100 he created the function array_multisearch. Your not going to find it in the manual :-)

Posted: Fri Nov 21, 2003 2:13 pm
by JayBird
hehehe, i c that now. time to get some sleep me thinks :)

Mark