You are viewing our Forum Archives. To view or take place in current topics click here.
Function php help??
Posted:
Function php help??Posted:
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Hey guys, I need some help with this problem, I am trying to develop a function that generates and returns 'n' sets of random lottery numbers as a 2 dimensional array. I also need to make use of another function I had created earlier on(below too). The code I have at the minute feels like its nearly there but i am struggling to finish it off, can anyone help me out so I can finish it off?
getRow function:
getSet 2 dimensional array:
getRow function:
function getRow()
{
$ball = [];
for($i=1;$i<=6;$i++){
$ball[] = rand(1,59);
}
sort($ball);
return $ball;
}
echo '<pre>';
print_r(getRow());
echo '</pre>';
getSet 2 dimensional array:
function getSet($n)
{
$n = array('rows'=>array(
getRow($ball)
),
'sets'=>array(
getSet($ball)
)
);
return $ball;
echo "<pre>look\n".
print_r(getSet(5));
'</pre>';
}
echo '<pre>';
#2. Posted:
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
First off I don't really know what you are trying to do,
but I think your problem is you are trying to push a number to the array, but you are doing it wrong.
Is this what you are trying to do?
with comments:
drier code:
but I think your problem is you are trying to push a number to the array, but you are doing it wrong.
Is this what you are trying to do?
with comments:
function getRow()
{
$ball = []; // clear the array & btw this line might not be needed
for($i=1;$i<=6;$i++){
$ball[$i] = rand(1,59);
}
// $ball = 6 random numbers
$ball = sort($ball); // put them in numerical order?
return $ball; // return an array of six random numbers
}
echo '<pre>';
print_r(getRow());
echo '</pre>';
drier code:
function getRow()
{
for($i=1;$i<=6;$i++){
$ball[$i] = rand(1,59);
}
return sort($ball);
}
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
CriticaI wrote First off I don't really know what you are trying to do,
but I think your problem is you are trying to push a number to the array, but you are doing it wrong.
Is this what you are trying to do?
with comments:
function getRow()
{
$ball = []; // clear the array & btw this line might not be needed
for($i=1;$i<=6;$i++){
$ball[$i] = rand(1,59);
}
// $ball = 6 random numbers
$ball = sort($ball); // put them in numerical order?
return $ball; // return an array of six random numbers
}
echo '<pre>';
print_r(getRow());
echo '</pre>';
drier code:
function getRow()
{
for($i=1;$i<=6;$i++){
$ball[$i] = rand(1,59);
}
return sort($ball);
}
Thanks for the reply, however I already done this part. This is the question I am trying to answer:
Develop a function getSet($n) that generates and returns n sets of random lottery ball numbers as a 2-dimensional array. Your solution to this task should make use of the getRow() function developed it task 2 to generate the rows in your 2-dimensional array. Again test the solution by making a call to the print_r(getSet(5)).
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
function getSet($numberOfSets){
for ($i=0; $i<$numberOfSets; $i++) {
$sets[$i] = getRow();
}
return $sets;
}
/*
getSet(3) would return something like this
[
[23, 56, 43, 1, 2, 32],
[58, 57, 45, 45, 21, 5],
[12, 10, 11, 8, 9, 16]
]
*/
$mysets = getSet(3);
$mysets[0] // returns [23, 56, 43, 1, 2, 32]
$mysets[0][4] // returns 2
- 1useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
CriticaI wrote
function getSet($numberOfSets){
for ($i=0; $i<$numberOfSets; $i++) {
$sets[$i] = getRow();
}
return $sets;
}
/*
getSet(3) would return something like this
[
[23, 56, 43, 1, 2, 32],
[58, 57, 45, 45, 21, 5],
[12, 10, 11, 8, 9, 16]
]
*/
$mysets = getSet(3);
$mysets[0] // returns [23, 56, 43, 1, 2, 32]
$mysets[0][4] // returns 2
Legendddddd ------ REPPP
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.