3

I am trying to write a christmas kris kringle buying list. Ie: Steve buys for Jo, Edward buys for Ally etc. I have 19 people to work with.

Currently I have completed two arrays. I used shuffle to shuffle the original names and have then added them together to store them into a multi dimensional array. It works.. but I have issues with.

  1. The shuffle doesn't care if the person buys their own present. (ie: the multi dimensional array has the same value. ie: [15] => Array ( [0] => Ben[1] => Ben)

  2. how would i go about making exceptions for family members, ie: ben can't buy for Ellen, dave and chilli? and so on.

I have tried looking at various array methods, but i have no more answers.

I would be thinking isomething like this.

if the values are the same, reshuffle and check again. else go to the next value and check again.

<?php

$peoplearray = Array("ben","peter","oscar","jake", "heidi", "Steve", "ed", "matt", "jordan", "gilly" , "Lea", "ellen", "dave", "chilli", "Sean", "Mark", "shane", "ali","dean");

shuffle($peoplearray);
$buying_for = array();
$k=0;

foreach($peoplearray as $value){
print "<BR>";
$buying_for[$k] = $value;
$k++;
}

$peoplearray = Array("ben","peter","oscar","jake", "heidi", "Steve", "ed", "matt", "jordan", "gilly" , "Lea", "ellen", "dave", "chilli", "Sean", "Mark", "shane", "ali","dean");


$total = count($peoplearray);


$result = array();
foreach ($peoplearray as $i => $val) {
    $result[] = array($val, $buying_for[$i]);

}
  for ($row = 1; $row < $total; $row++) {
  echo "<p><b>Pair $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 2; $col++) {
    echo " ".$result[$row][$col]." ";
  }
  echo "</ul>";
}


?>
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – i am me Nov 29 '15 at 09:56

1 Answers1

0

This does a fairly good job of mixing things up when it comes to matching buyers with recipients - not entirely sure it is what you are after but it might be of use.

    $people = array(
        'ben', 'peter', 'oscar', 'jake',
        'heidi', 'Steve', 'ed', 'matt',
        'jordan', 'gilly' , 'Lea', 'ellen', 
        'dave', 'chilli', 'Sean', 'Mark',
        'shane', 'ali', 'dean', 'bert', 
        'andrew', 'isabelle', 'laura', 'rebecca',
        'susan', 'huda', 'hazel', 'una',
        'isla', 'kerry', 'helen', 'thomas'
    );
    $matches=array();
    if( count( $people ) % 2 > 0 ) exit('There is an odd number of people - somebody would be missed.');

    function test($recipient,$buyer){
        return $recipient!==$buyer;
    }
    function implodearray( $array, $newline=false, $sep='=', $delim='&', $tabs=0 ){
        $tmp=array();
        if( is_array( $array ) && !empty( $array ) ){
            foreach( $array as $key => $value ) $tmp[]=$key.$sep.$value;
            $delimiter = $newline ? $delim . PHP_EOL : $delim;
            return implode( $delimiter . str_repeat( chr(9),$tabs ), $tmp );
        }
        return false;
    }

    foreach( $people as $index => $buyer ){
        $i = rand( 0, count( $people )-1 );
        while( $i==$index ) $i = rand( 0, count( $people )-1 );

        $matches[ ucfirst( strtolower( $buyer ) ) ]=ucfirst( strtolower( $people[ $i ] ) );
        array_splice( &$people, $i, 1 );
    }

    echo '<pre>',implodearray( $matches, 0,' buys for ', PHP_EOL),'</pre>';
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46