5 Replies - 479 Views - Last Post: 15 September 2012 - 12:53 PM Rate Topic: -----

#1 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

How do I strip the keys out of an array and display the values in HTML

Posted 14 September 2012 - 04:03 PM

I am working on a website that rolls X dice of Y sides and stores the results in an array.

I am using a for loop to create the array and testing with printr has shown that the rolls are happening correctly and the data is being parsed to the array correctly as well.

My problem is I want to take the array, strip off the keys and print all the results in the webpage as plain text.

Here us what I got so far:

else {
for ($i = 0; $i != $numberofdice; $i++)
$result[] = rand(1, $valueofdice);
}



Help!

I can't edit my original post but the code should be like this:

else {
for ($i = 0; $i != $numberofdice; $i++)
$result[] = rand(1, $valueofdice);
}



Is This A Good Question/Topic? 0
  • +

Replies To: How do I strip the keys out of an array and display the values in HTML

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: How do I strip the keys out of an array and display the values in HTML

Posted 14 September 2012 - 04:18 PM

array_values
Was This Post Helpful? 1
  • +
  • -

#3 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: How do I strip the keys out of an array and display the values in HTML

Posted 14 September 2012 - 04:31 PM

View PostJackOfAllTrades, on 14 September 2012 - 04:18 PM, said:



I looked at the examples on the site you linked to but I don't understand what I'm supposed to plug in for $array as $key => $value?

I should also mention I am trying to get rid of the index and just extract the values for display on the webpage.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: How do I strip the keys out of an array and display the values in HTML

Posted 14 September 2012 - 06:38 PM

You don't need to plug anything in. You have an array.
If you're going to use PHP, you MUST become comfortable with arrays, so I would suggest studying the documentation.
Was This Post Helpful? 0
  • +
  • -

#5 dallbee  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 15
  • Joined: 17-October 11

Re: How do I strip the keys out of an array and display the values in HTML

Posted 15 September 2012 - 12:19 PM

Bit of hand-holding:
<?php

$dice_count = 5;
$dice_sides = 6;
$results = array();

for($i = 0; $ < $dice_count; ++$i)
{
    $results[$i] = rand(1, $dice_sides);
}

foreach($results as $key => $value)
{
    echo "{$key}: {$value}, ";
}

// end of script


Was This Post Helpful? 0
  • +
  • -

#6 aguyfromhere  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-April 12

Re: How do I strip the keys out of an array and display the values in HTML

Posted 15 September 2012 - 12:53 PM

View Postdallbee, on 15 September 2012 - 12:19 PM, said:

Bit of hand-holding:
<?php

$dice_count = 5;
$dice_sides = 6;
$results = array();

for($i = 0; $ < $dice_count; ++$i)
{
    $results[$i] = rand(1, $dice_sides);
}

foreach($results as $key => $value)
{
    echo "{$key}: {$value}, ";
}

// end of script



Thank you so much for the help. After many hours of reading and experimenting I came up with this last night.

function result($result, $numberofdice, $valueofdice) {
if ((empty($numberofdice)) and (empty($valueofdice))) {
	echo "";
}
elseif ((( ! is_numeric($numberofdice)) or ($numberofdice > 10)) or ($numberofdice < 0)) {
	echo "The number of dice you can roll must be a whole number between 1 and 10.";
	}
elseif ((( ! is_numeric($valueofdice)) or ($valueofdice > 20)) or ($valueofdice < 0)) {
	echo "The value of dice you can roll must be a whole number between 1 and 20.";
	}
else {
	for ($i = 0; $i != $numberofdice; $i++) {
	$result[] = mt_rand(1, $valueofdice);
	}
	$arrSize = sizeof($result);
	echo "You rolled $numberofdice, $valueofdice sided dice. Here are the results in roll order: <br />";
	for ($number = 0; $number < $arrSize; $number++) {
	echo "$result[$number]";
	if (($number + 1) == $arrSize) {
	echo ".";
	}
	else {
	echo ", ";
	}
	}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1