I am trying to make card game.
I have 6 variable that are stored in array. Than I use fisherYates method to randomize array, and display four of them.
Problem is, when I randomize it this way only, it will give only random output of those six, with all different types.
So I want that some repeats like, if you draw four cards, you get output of
ex: club, club, diamond,heart, or heart, star,star,heart.. if you get a point..
I thought to do it like this way: put the array in loop of 4 times, and every time it loops, it stores first, or last value in new array, so that way, I can have greater chances of combination of same cards in output array.
But I'm stuck, and I don't know how to do it :/
this is what I've tried so far
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
reset($niz);
$i++;
} while ($i <= 3);
Got a solution. Was just to simply do foreach of first element of multidimensional array :)
Code goes like this:
$diamond = 'cube.jpg';
$heart = 'heart.jpg';
$spade = 'spade.jpg';
$club = 'tref.jpg';
$star='star.jpg';
$qmark='qmark.jpg';
$time=microtime(35);
$arr=[$diamond,$heart,$spade,$club,$star,$qmark];
$niz=array();
$i=0;
do {
$niz[$i]=fisherYatesShuffle($arr,$time);
//reset($niz);
$i++;
} while ($i <= 3);
foreach ($niz as $key ) {
$randomArr[]=$key[0]; ;
}
function fisherYatesShuffle(&$items, $time)
{
for ($i = count($items) - 1; $i > 0; $i--)
{
$j = #mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
return $items;
}
print_r($randomArr);
I have have these for loops:
for ($x = 0; $x<2;$x++){
$allproducts = array();
for ($y = 0; $y<105;$y++) { //<<<<<<<<<<<<< this (105) is the number I mean
$allproducts[] = 'test'.$y;
}
echo "<pre>";
echo "allproducts 0 -- ";
var_dump($allproducts);
echo "</pre>";
for ($i = 0; $i < count($allproducts); $i++) {
$result = array_slice($allproducts, 0, 20);
echo "<pre>";
echo "result -- ";
var_dump($result);
echo "</pre>";
$allproducts = (array_diff($allproducts, $result));
echo "<pre>";
echo "allproducts 1 -- ";
var_dump($allproducts);
echo "</pre>";
}}
In real code (here:)
for ($x = 0; $x<1;){
$allproducts = array();
$abfrage = "SELECT sku FROM skulist";
if ($result = $sql->query($abfrage))
{
while ($row = $result->fetch_assoc()) {
$allproducts[] = $row['sku'];
}
}
for ($i = 0; $i < count($allproducts); ++$i) {
$result = array_slice($allproducts, 0, 20);
$allproducts = (array_diff($allproducts, $result));
//.... more code
}}
...I take data from DB instead of this for loop:
$allproducts = array();
for ($y = 0; $y<105;$y++) { //<<<<<<<<<<<<< this (105) is the number I mean
$allproducts[] = 'test'.$y;
}
but changed it to show/ for easier reconstruction.
The behavior is the same:
When I have 105 entries in my array, I don't get an empty array output for
echo "allproducts 1 -- ";
var_dump($allproducts);
which is the desired behavior.
When I change 105 to (eg.) 65, it outputs an empty arrays
allproducts 1 -- array(0)
at the end of the second nested loop:
for ($i = 0; $i < count($allproducts); $i++) {
Now I have three problems:
why does this happen? I don't get the difference
how can I achieve, that there is no empty array at the end? (stops my programs work)
I need 20 values in every $result array
for ($i = 0; $i < count($allproducts); $i++) {
// ...
$allproducts = (array_diff($allproducts, $result));
You are changing your $allproducts array while going through it! How can you expect it to work reliably then?
Instead, try:
foreach( $allproducts as $i => $product)
Although I must admit I'm not sure what you're trying to do.
I'm new on PHP and I want to find the 0 and replace with the number that is missed, inside the inner array, on a multidimensional array. If the inner array has more than two 0's, it will be ignored and goes to the next.
$list = array("First"=>array(0,1,2,3,0,5,6,7,8,9),
"Second"=>array(0,1,2,3,4,5,6,7,8,9),
"Third"=>array(0,1,2,3,4,5,0,0,8,9),
"Fourth"=>array(0,1,2,3,4,5,6,7,8,0),
"Fifth"=>array(0,1,2,3,4,5,0,7,8,9),
"Sixth"=>array(0,0,0,3,4,5,6,0,0,0),
"Seventh"=>array(0,1,2,3,0,0,6,7,8,9),
"Eighth"=>array(0,1,2,3,4,5,0,7,8,9),
"Ninth"=>array(0,1,2,3,4,0,6,7,8,9),
"Tenth"=>array(0,0,2,3,4,5,6,7,8,9));
$countZero = 0;
foreach($list as $lvl) {
foreach($lvl as $ind => $val) {
if($countZero = array_count_values($lvl[$val] === 0))
$list[$ind][$val] = 45 - array_sum($ind);
echo $count;
}
}
I want all inner arrays, that have two 0's get only one, to have all numbers in sequence i.e.
"First"=>array(0,1,2,3,4,5,6,7,8,9);
Please, help me.
I tried this code below, trying to finde the 0's.
$counts = 0;
$newArr = array();
foreach($list as $lvl) {
if(is_array($lvl)) {
for($i = 0; $i < count($lvl) - 1; $i++) {
if(($lvl[$i] == 0) < 2){
$counts++;
$newArr[$i] = 45 - array_sum($lvl);
}
}
}
}
print_r($newArr);
This is a solution using array_walk:
array_walk($list,
function(&$numbers) {
$zeroIndex = 0;
foreach($numbers as $i => $number) {
if( $number === 0 ) {
if( $zeroIndex > 0 ) {
return;
}
$zeroIndex = $i;
}
}
$numbers[$zeroIndex] = $zeroIndex;
});
You don't need to count all the zeros. You just need to check if there are less than 3 zeros.
I'm saving the index (position) of zero ($zeroIndex = $i).
I'm assuming that the first number is always a zero ($zeroIndex = 0).
The index of the second zero is greater than zero. If I find a zero when the index of the last found zero is greater than zero (if( $zeroIndex > 0 )), this means that there are more than two zeros.
In fact,here is what I've done and worked.
$list = array(array(1,2,3,0,5,6,7,8,9),
array(1,2,3,4,5,6,7,8,9),
array(1,2,3,4,5,0,0,8,9),
array(1,2,3,4,5,6,7,8,0),
array(1,2,3,4,5,0,7,8,9),
array(0,0,3,4,5,6,0,0,0),
array(1,2,3,0,0,6,7,8,9),
array(1,2,3,4,5,0,7,8,9),
array(1,2,3,4,0,6,7,8,9));
for($l = 0; $l < count($list); $l++)
{
$total = 0;
$countZ = 0;
for($i=0; $i < 9; $i++)
{
if($list[$l][$i] == 0)
{
$countZ++;
$indexZero = $i;
}
$total += $list[$l][$i];
if($countZ > 1) {
break;
}
}
$list[$l][$indexZero] = 45 - $total;
}
print_r($list);
TY all.
I've got a form with input names like price1, price2, price3 and I'm trying to get these values in another page. I'm sending it using GET.
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price" + $i + "'];
echo $price;
}
How should I declare the $price variable?
Need to put . in place of + sign...
for ($i = 1; $i < $qtd_itens; $i++) {
$price = $_GET['price'. $i. ''];
echo $price;
}
If you just want to collect all the prices you can do:
$prices = array();
for ($i = 1; $i < $qtd_itens; $i++) {
$prices[] = $_GET["price" . $i];
}
var_dump($prices);
To clarify:
$arr[] = "a";
is shorthand for:
array_push($arr, "a");
or
$arr[$i] = "a";
If you form input having price1, price2, price3... etc, you simply make this for view content of data send:
print_r($_REQUEST)
This show you data in array send, and collect with foreach with this:
$prices = array();
foreach ($_REQUEST as $key=>$val) {
$prices[$key] = $val;
}
print_r($prices);