jacob1986 Posted October 16, 2015 Share Posted October 16, 2015 I have to complete a simple task which says 'use foreach to print the value of the sum of two cards, the deck has 16 cards and each card has one number and one colour'. Moreover; I can write four cards and four numbers, but how do I write all sixteen cards (I would prefer if they could sit abreast - parallel to each other)? The code I have so far is this: <?php$a = array( "Red" => 1, "Blue" => 2, "Green" => 3, "Yellow" => 4,);foreach ($a as $key => $val) { echo "<h4>$key => $val.</h4>";}?> // Output: Red => 1. Blue => 2. Green => 3. Yellow => 4. Quote Link to comment Share on other sites More sharing options...
Strider64 Posted October 16, 2015 Share Posted October 16, 2015 (edited) This isn't going to totally answer you question, but it might. What I do is I set up a mock HTML page with the proper tags and CSS to it. Then I add the PHP (or whatever language you using) to that mockup, for example I did that with this page : http://www.pepster.com/calendar.php?urlDate=2015-10-16&page=0 It'll make you should make your task simpler and actually it isn't that more work. Just look at the source code (HTML / CSS) of that page and it should give you a general idea in how to do it. Edited October 16, 2015 by Strider64 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16, 2015 Share Posted October 16, 2015 This isn't going to totally answer you question... You know what the question is? 1 Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted October 16, 2015 Author Share Posted October 16, 2015 Yes, I was a bit confused regarding the question, I need to print two cards from the deck? <?php $array = [['Red => 4'],]; foreach ($array as list($a,)) { echo "Card One: $a."; } $array = [['Blue => 2'],]; foreach ($array as list($a,)) { echo "Card Two: $a."; } ?> //Output: Card One: Red => 4.Card Two: Blue => 2. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16, 2015 Share Posted October 16, 2015 I'm still not sure what you want to do (sum of which two cards?). But you could define the stack of cards like below (array keys have to be unique but not values). $cards = array ( 1 => 'Red', 2 => 'Yellow', 3 => 'Green', 4 => 'Red', 5 => 'Yellow', 6 => 'Blue', 7 => 'Yellow', 8 => 'Red', 9 => 'Yellow', 10 => 'Blue', 11 => 'Yellow', 12 => 'Green', 13 => 'Blue', 14 => 'Red', 15 => 'Blue', 16 => 'Green' ); Quote Link to comment Share on other sites More sharing options...
hansford Posted October 16, 2015 Share Posted October 16, 2015 (edited) I don't know what you want either, but just for fun. <?php error_reporting(E_ALL); ini_set('display_errors',1); $cards = array( "Red" => 1, "Blue" => 2, "Green" => 3, "Yellow" => 4, "Black" => 5, "White" => 6, "Aqua" => 7, "Fuchsia" => 8, "Gray" => 9, "Lime" => 10, "Maroon" => 11, "Navy" => 12, "Olive" => 13, "Purple" => 14, "Silver" => 15, "Teal" => 16, ); $array2 = shuffle_array($cards); $keys = array_keys($array2); $vals = array_values($array2); $i = 0; foreach ($cards as $key => $value) { $sum = $value + $vals[$i]; echo "$key => $value + {$keys[$i]} => {$vals[$i++]} = $sum<br />"; } function shuffle_array( $arr ) { $keys = array_keys($arr); shuffle($keys); $values = array_values($arr); $arg = array(); foreach($keys as $key) { $arg[$key] = $arr[$key]; } return $arg; } Edited October 16, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted October 17, 2015 Author Share Posted October 17, 2015 I think this may be the correct way... sixteen cards all together - two random cards picked! <?php$input = array("Red => 1","Blue => 2","Green => 3","Yellow => 4","Red => 5","Blue => 6","Green => 7","Yellow => 8","Red => 9","Blue => 10","Green => 11","Yellow => 12","Red => 13","Blue => 14","Green => 15","Yellow => 16",);$rand_keys = array_rand($input, 2);echo $input[$rand_keys[0]] . "\n";echo $input[$rand_keys[1]] . "\n";?>//Output:Blue => 10 Yellow => 16 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 17, 2015 Share Posted October 17, 2015 Yes it picks two random values. But you cant "get the sum of the two cards", You need to set your array as Barand suggested provided each cards value is unique for every color. Example code for picking two random values using array_rand and getting the sum of the chosen cards $cards = array ( 1 => 'Red', 2 => 'Yellow', 3 => 'Green', 4 => 'Red', 5 => 'Yellow', 6 => 'Blue', 7 => 'Yellow', 8 => 'Red', 9 => 'Yellow', 10 => 'Blue', 11 => 'Yellow', 12 => 'Green', 13 => 'Blue', 14 => 'Red', 15 => 'Blue', 16 => 'Green' ); $values = array_rand($cards, 2); echo "The two card picked are:<br />"; echo $cards[$values[0]] . " value is $values[0]<br />"; echo $cards[$values[1]] . " value is $values[1]<br />"; $sum = $values[0] + $values[1]; echo "The sum of the two cards picked is: $sum"; Quote Link to comment Share on other sites More sharing options...
hansford Posted October 17, 2015 Share Posted October 17, 2015 (edited) I thought you needed the sum of the 2 cards as well. How does this accomplish that task. Ok, Ch0cu3r has already solved this for you. Moving on. Edited October 17, 2015 by hansford Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted October 17, 2015 Author Share Posted October 17, 2015 Thank-you Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 17, 2015 Share Posted October 17, 2015 (edited) @my2cents: For noobies, allow me to introduce array_sum. Yet another way to get the same result. $cards = array ( 1 => 'Red', 2 => 'Yellow', 3 => 'Green', 4 => 'Red', 5 => 'Yellow', 6 => 'Blue', 7 => 'Yellow', 8 => 'Red', 9 => 'Yellow', 10 => 'Blue', 11 => 'Yellow', 12 => 'Green', 13 => 'Blue', 14 => 'Red', 15 => 'Blue', 16 => 'Green' ); $values = array_rand($cards, 2); ; echo "The two card picked are:<br />"; echo $cards[$values[0]] . " value is $values[0]<br />"; echo $cards[$values[1]] . " value is $values[1]<br />"; echo "The sum of the two cards picked is:" .array_sum($values); If you do not need to know the individual values being summed you could just do: $values = array_rand($cards, 2); echo $cards[$values[0]] . " value is $values[0]<br />"; echo $cards[$values[1]] . " value is $values[1]<br />"; echo "The sum of the two cards picked is:" .array_sum(array_rand($cards, 2)); Edited October 17, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17, 2015 Share Posted October 17, 2015 If you don't need to know what the two cards are then the program can reduce to a single line echo rand(3,31); Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 17, 2015 Share Posted October 17, 2015 (edited) If you don't need to know what the two cards are then the program can reduce to a single line echo rand(3,31); Well, no. The task was to sum two cards. print the value of the sum of two cards Edited October 17, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17, 2015 Share Posted October 17, 2015 I know. The sum of two cards numbered 1 - 16 would be a number between 3 and 31. Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 17, 2015 Share Posted October 17, 2015 Ahhh, very clever Barand. Good one. If you don't need to know the card color, the card array is not even needed. 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.