Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/
Share on other sites

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 by Strider64
Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523482
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523488
Share on other sites

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' 
    );
Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523494
Share on other sites

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 by hansford
Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523508
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523542
Share on other sites

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";
Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523547
Share on other sites

@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 by benanamen
Link to comment
https://forums.phpfreaks.com/topic/298647-foreach-arrays/#findComment-1523556
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.