Schlo_50 Posted November 13, 2009 Share Posted November 13, 2009 Hello, I have 2 arrays; $carList which contains 3 cars and their attributes, and $playerSelected which contains details of 1 car belonging to a human player. Arrays: <?php $carList = array( array( 'manufacturer' => 'Mitsubishi', 'model' => 'Colt', 'modexp' => '1200', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ), array( 'manufacturer' => 'Mitsubishi', 'model' => 'Evolution', 'modexp' => '45000', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ), array( 'manufacturer' => 'Toyota', 'model' => 'MR2', 'modexp' => '25000', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ) ); $playerSelected = array( 'manufacturer' => 'Toyota', 'model' => 'Celica', 'modexp' => '1200000', 'traction' => '500000', 'aerodynamics' => '3000000', 'acceleration' => '1500000', 'styling' => '6000000' ); /> In my code there is a function to randomly pick 2 cars from the $carList array. What I want to do is after the random pair of cars are generated, combine my $playerSelected array into that. I've tried array_push and array_merge but can't quite get it right. Function: <?php function getRandomSubset($carList, $n) { /* * If $n isn't greater than the number of entries, * reset it to be the number of entries available. */ $n = (count($carList)<$n) ? count($carList) : $n; $randList = array(); // Pull the desired number of entry keys at random $keys = array_rand($carList, $n); // Loop through the keys foreach($keys as $key) { // Add the value to end of the return array array_push($randList, $carList[$key]); } //My not so great attempt on the next line.. array_push($randList, $playerSelected); return $randList; } echo '<pre>'; //This just displays the two randomly generated cars $randomSet = getRandomSubset($carList, 2); echo '</pre>'; /> Is there anyone who can help me please? If you need more info just say! Thanks Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 13, 2009 Author Share Posted November 13, 2009 Apologies in advance for not closing my PHP tags properly. No idea why I did that.. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 13, 2009 Share Posted November 13, 2009 What do you mean by 'combine'? Do you with your player array to contain two cars afterwards? If so, you need to make your player array a two dimensional one. I.e: $playerSelected = array(); $playerSelected[] = array( 'manufacturer' => 'Toyota', 'model' => 'Celica', 'modexp' => '1200000', 'traction' => '500000', 'aerodynamics' => '3000000', 'acceleration' => '1500000', 'styling' => '6000000' ); You can then add a new car in the same vain as above; $playerSelected[] = $carList[$key]; Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 13, 2009 Share Posted November 13, 2009 What you currently have should work if you pass the array stored in $playerSelected to the function. In your current code $playerSelected isn't available to the function as a function has it's own scope. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 13, 2009 Author Share Posted November 13, 2009 Well, my function will output 2 random cars from the initial 3 like this: Array ( [0] => Array ( [manufacturer] => Toyota [model] => Aygo [modexp] => 950 [traction] => 500 [aerodynamics] => 3000 [acceleration] => 1500 [styling] => 6000 ) [1] => Array ( [manufacturer] => Nissan [model] => 350Z [modexp] => 30000 [traction] => 500 [aerodynamics] => 3000 [acceleration] => 1500 [styling] => 6000 ) ) I want to add my players car to the array above to end up with this: Array ( [0] => Array ( [manufacturer] => Toyota [model] => Aygo [modexp] => 950 [traction] => 500 [aerodynamics] => 3000 [acceleration] => 1500 [styling] => 6000 ) [1] => Array ( [manufacturer] => Nissan [model] => 350Z [modexp] => 30000 [traction] => 500 [aerodynamics] => 3000 [acceleration] => 1500 [styling] => 6000 ) [2] => Array ( [manufacturer] => Toyota [model] => Celica [modexp] => 1200000 [traction] => 5000000 [aerodynamics] => 3000000 [acceleration] => 15000000 [styling] => 6000000 ) ) Quote Link to comment Share on other sites More sharing options...
Andy-H Posted November 13, 2009 Share Posted November 13, 2009 <?php function getRandomSubset($carList, $playerSelected, $n) { /* * If $n is greater than the number of values in the array, * reset it to be the number of entries available. */ $i = count($carList) - 1; $n = ($i < $n) ? $i : $n; $randList = array(); // Pull the desired number of entry keys at random $keys = array_rand($carList, $n); // Loop through the keys foreach($keys as $key) { // Add the value to end of the return array array_push($randList, $carList[$key]); } //Push the $playerSelected array on to the generated array array_push($randList, $playerSelected); return $randList; } // Set arrays of cars $carList = array( array( 'manufacturer' => 'Mitsubishi', 'model' => 'Colt', 'modexp' => '1200', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ), array( 'manufacturer' => 'Mitsubishi', 'model' => 'Evolution', 'modexp' => '45000', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ), array( 'manufacturer' => 'Toyota', 'model' => 'MR2', 'modexp' => '25000', 'traction' => '500', 'aerodynamics' => '3000', 'acceleration' => '1500', 'styling' => '6000' ) ); $playerSelected = array( 'manufacturer' => 'Toyota', 'model' => 'Celica', 'modexp' => '1200000', 'traction' => '500000', 'aerodynamics' => '3000000', 'acceleration' => '1500000', 'styling' => '6000000' ); // Generate car array $randomSet = getRandomSubset($carList, $playerSelected, 2); echo '<pre>'; /* This just displays the two randomly generated cars And the selected players car... */ print_r($randomSet); echo '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 13, 2009 Author Share Posted November 13, 2009 All makes sense now! Thanks for both you guys help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.