Related
Lets say for $numArr = array(10,20,30,40,50,60,70,80,90,100);
how to sum between specific range like 30 - 60 to get sum of 180.. (30+40+50+60).
edit : This is my latest code
<?php
function sum_array ($no1, $no2){
$array = array(10,20,30,40,50,60,70,80,90,100);
$input = array_slice($array, $no1, $no2);
return array_sum($input);
}
echo sum_array(0,3);
?>
I made a basic function for this according to u guys replies .. though still i want to put some validations into this like the parameters should be positive else the function should return -1 .. and what if the second index of the range is not in array.. like (90-120). Would it able to still sum what's in range and in array .. and give 190 to the above range.
$numArr = [ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
$startValue = 30;
$endValue = 60;
$startIndex = array_search($startValue, $numArr);
$endIndex = array_search($endValue, $numArr);
$length = $endIndex - $startIndex + 1;
$result = array_sum(array_slice($numArr, $startIndex, $length));
print_r($result);
Today my friend raised a challenge that I still can't solve: "Generate a random digit sequence in PHP"
The digits are arranged as dial-pad/pattern-lock that consist 1-9 keys in 3 rows and 3 columns:
---------------------------
| |
| 1 2 3 |
| |
| 4 5 6 |
| |
| 7 8 9 |
| |
---------------------------
Now, given a length, we have to generate a random, non-repeating sequence of digits of the provided length, using these criteria:
A generated sequence should follow a specific direction/pattern going only via neighboring digits (possibly diagonally), for example (length:8), 12569874:
1 🡪 2
🡫
4 5 🡪 6
🡩 🡫
7 🡨 8 🡨 9
Digits from the first row should never be followed by a digit from the third row, and vice-versa. The same goes for columns. For example a 1 cannot be followed by a 8, and a 6 cannot be followed by a 4.
can guess more criteria can easily from android pattern-lock system
Here are some example generated sequences for length 9: 12369874/5, 142536987, etc, and for length = 6: 987532, etc
I tried to do this with rand():
$chars = "123456789";
$length = 9;
$clen = strlen( $chars )-1;
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $chars[mt_rand(0,$clen)];
}
return ($id);
but, still no luck...
How can I solve this question?
has some limitations but that's for you to work out. I only deal with headaches when I get paid :).
<pre>
<?php
// Keypad
$grid = [
['1', '2', '3'],
['4', '5', '6'],
['7', '8', '9'],
];
// Sequence Target Length
$target_length = 5;
// Place to store the Keypad sequence
$points = [];
// Starting Point
$x = rand(0, 2);
$y = rand(0, 2);
// Run through the process until we have the sequence at the desired length
while (count($points) < $target_length):
// Check if the grid keypad entry has been used
if ($grid[$x][$y]):
// Hasn't been used, so stire it
$points[] = $grid[$x][$y];
// Mark it used
$grid[$x][$y] = NULL;
endif;
// Sanity Check, imagine if you will,.... target length of 9, and you hit 6 5 2 1, You'll vault off into the twilight zone without this
if ((!$grid[$x + 1][$y]) && (!$grid[$x][$y + 1]) && (!$grid[$x - 1][$y]) && (!$grid[$x][$y - 1])):
// We have no where to go
break;
endif;
// Start looking for possible values
do {
$test_x = $x;
$test_y = $y;
$dir = rand(0, 3);
switch ($dir):
case (0):
$test_y--; // Up
break;
case (1):
$test_x++; // Right
break;
case (2):
$test_y++; // Down
break;
case (3):
$test_x--; // Left
break;
endswitch;
// Optional Gibberish
echo "Moving from {$x}, {$y} to {$test_x}, {$test_y} --> " . (($grid[$test_x][$test_y] === NULL) ? 'FAILED' : 'OK!') . '<br>';
// Keep going until we find a valid direction
} while ($grid[$test_x][$test_y] === NULL);
// assign the new coords
$x = $test_x;
$y = $test_y;
// repeat
endwhile;
// report
echo implode('-', $points) . "\n";
?>
</pre>
Here is a solution that will apply these rules:
a path can only step to neighboring cells, i.e. that are adjacent, including diagonally
a path cannot contain the same cell twice
The following algorithm uses recursion for every digit that is added to the sequence. Whenever the sequence gets "stuck", backtracking happens, and an alternative path is tried. Backtracking continues if no more alternatives are available.
It is guaranteed that a path of the given length is returned, provided the given length is between 1 and 9:
function randomSequence($len) {
if ($len < 1 || $len > 9) return []; // No results
$row = [null, 1, 1, 1, 2, 2, 2, 3, 3, 3];
$col = [null, 1, 2, 3, 1, 2, 3, 1, 2, 3];
$neighbors = [[], [2, 4, 5], [1, 4, 5, 6, 3], [2, 5, 6],
[1, 2, 5, 7, 8], [1, 2, 3, 4, 6, 7, 8, 9], [2, 3, 5, 8, 9],
[4, 5, 8], [4, 5, 6, 7, 9], [5, 6, 8]];
// Shuffle the neighbor lists to implement the randomness:
foreach ($neighbors as &$nodes) shuffle($nodes);
$recurse = function ($seq) use (&$len, &$row, &$col, &$neighbors, &$recurse) {
if (count($seq) >= $len) return $seq; // found solution
$last = end($seq);
echo "try " . json_encode(array_keys($seq)) . "\n";
foreach ($neighbors[$last] as $next) {
if (isset($seq[$next])) continue; // Skip if digit already used
$result = $recurse($seq + [$next => $next]);
if (is_array($result)) return $result;
}
};
$choice = rand(1, 9);
return array_keys($recurse([$choice => $choice]));
}
echo "result: " . json_encode(randomSequence(9)) . "\n";
See it run on repl.it
Here's an example in pseudocode for a matrix that looks like this:
1 2
3 4
# Get which other numbers are "legal moves" from each number.
adjacency = {
1: [2, 3],
2: [1, 4],
3: [1, 4],
4: [2, 3]
}
# Get the length of code required.
n = 8
# Start at a random position;
pos = rand(keys(adjacency))
result = []
while (n > 0)
n -= 1
newpos = rand(adjacency[pos])
result[] = newpos
pos = newpos
print(result.join(', '))
If your matrix is going to be large or is going to vary you might want to write some code to generate adjaceny rather than hardcoding it.
I would like to ask how I can get the length of digits in an Integer. For example:
$num = 245354;
$numlength = mb_strlen($num);
$numlength should be 6 in this example. Somehow I can't manage it to work?
Thanks
EDIT: The example code above --^ and its respective method mb_strlen(); works just fine.
Maybe:
$num = 245354;
$numlength = strlen((string)$num);
Accepted answer won't work with the big numbers. The better way to calculate the length of any number is to invoke floor(log10($num) + 1) with a check for 0.
$num = 12357;
echo $num !== 0 ? floor(log10($num) + 1) : 1; // prints 5
It has multiple advantages. It's faster, you don't do the casting of types, it works on big numbers, it works with different number systems like bin, hex, oct.
The equation does the logarithm with base 10 then makes the floor of it and adds 1.
This solution can work independently on the base, so if you want to calculate the length of binary or hex just change the base of the logarithm.
Working fiddle
The accepted solution presents a problem when evaluating negative numbers.
It works with a positive number:
$num = 245354;
$numlength = strlen((string)$num);
// Result: 6
But with a negative number, the (-) is added to the count:
$num = -245354;
$numlength = strlen((string)$num);
// Result: 7
Quick workaround:
$num = -245354;
$numlength = strlen((string)abs($num));
// Result: 6
More elegant way :)
ceil(log10($num));
You could also use some basic math!
$digits = (int)(log($num,10)+1)
<?php
$num = 123;
$num2 = 1234;
$num3 = 12345;
function digits($num){
return (int) (log($num, 10) + 1);
}
echo "\n $num: " . digits($num); // 123: 3
echo "\n $num2:" . digits($num2); // 1234: 4
echo "\n $num3:" . digits($num3); // 12345: 5
echo "\n";
Another way to find out the length of a number in digits would be to divide the integer part of the number to 10 until it becomes 0.
Example:
2021/10 = 202.1
202/10 = 20.2
20/10 = 2
2/10 = 0.2
Code:
function numberGetLength($number) {
$count = 0;
while (intval($number) > 0) {
$number = intval($number) / 10;
$count += 1;
}
return $count
}
Just using some version of (int)(log($num,10)+1) fails for 10, 100, 1000, etc. It counts the number 10 as 1 digit, 100 as two digits, etc. It also fails with 0 or any negative number.
If you must use math (and the number is non-negative), use:
$numlength = (int)(log($num+1, 10)+1);
Or for a math solution that counts the digits in positive OR negative numbers:
$numlength = ($num>=0) ? (int)(log($num+1, 10)+1) : (int)(log(1-$num, 10)+1);
But the strlen solution is just about as fast in PHP.
In PHP types are loosely set and guessed, if you want to see something as a string if it is an integer, float, and (i have not tried this) bool then #Gorjunav is the most correct answer.
Reset the variable as a string
$stringNum = (string) $num;
Then you can go anything string related you want with it! And vice-versa for changing a string to an int
$number = (int) $stringNum;
and so on...
count only integer value
`<?php
$n1 =12345;
$n2 =123454.55;
$n3 =12345564.557;
echo "The Number you Type: ".$n1."<br>";
$count = 0;
while ($n1 != 0)
{
$n1 = $n1 / 10;
$n1 = intval($n1);
++$count;
}
echo "The Digit in a Number: ".$count;
}
?>`
echo strlen((string) abs($num)); // using **abs** it'll work with negative integers as well
Tested in PHP 4.4.9 - 8.0.0
$array = array(-1, 0, -0, 1, 4, 9, 10, -10, 20, -20, 100, -100);
foreach( $array as $key => $num ){
echo $key."\t{$num}\t=>\t".($num !== 0 ? floor(log10(abs($num)) + 1) : 1)."\n";
}
/* Output:
0 -1 => 1
1 0 => 1
2 0 => 1
3 1 => 1
4 4 => 1
5 9 => 1
6 10 => 2
7 -10 => 2
8 20 => 2
9 -20 => 2
10 100 => 3
11 -100 => 3
*/
The following function work for either integers or floats (works with PHP7+):
function digitsCount($number): int
{
$number = abs($number);
$numberParts = explode(".", $number);
return
strlen($numberParts[0]) +
(strlen($numberParts[1] ?? 0));
}
Problem:
I have a field in my MySQL table with the following value:
9, 10, 11, 12, 13, 14, 15, 16, 26, 27, 28, 29, 30, 31, 32
I use PHP to put the value of this field in the variable: $row['Exclude'];
The problem is that I am using a function called rand_except() that looks as following:
function rand_except($min, $max, $except)
{
//first sort array values
sort($except, SORT_NUMERIC);
//calculate average gap between except-values
$except_count = count($except);
$avg_gap = ($max - $min + 1 - $except_count) / ($except_count + 1);
if ($avg_gap <= 0)
return false;
//now add min and max to $except, so all gaps between $except-values can be calculated
array_unshift($except, $min - 1);
array_push($except, $max + 1);
$except_count += 2;
//iterate through all values of except. If gap between 2 values is higher than average gap,
// create random in this gap
for ($i = 1; $i < $except_count; $i++)
if ($except[$i] - $except[$i - 1] - 1 >= $avg_gap)
return mt_rand($except[$i - 1] + 1, $except[$i] - 1);
return false;
}
In order for this to work it needs to be like this:
$exclude = array(9, 10, 11, 12, 13, 14, 15, 16, 26, 27, 28, 29, 30, 31, 32);
$_SESSION['experimentversion'] = rand_except(1, 32, $exclude);
Question:
How can I take the database field $row['Exclude'] and transform it into an array so it will work with the function?
Simple. Use Explode function.
$s = "1,2,3,4";
$y = explode(",", $s);
print_r($y)
There is a explode method in php you can use this method
$string = '1,2,3,4,5';
$array = explode(",",$string);
print_r($array);
it will create an array.
$exclude = explode(', ', $row['Exclude']);
$str = '1,2,3,4,5';
$arr = explode(",",$str);
print_r($arr);
This should do the trick:
$exclude = explode(", ", $row['Exclude']);
use explode function.. for more info of Explode Visi this link
$row = "retrive your value from db";
$data = explode(", ",$row);
print_r($data); //here you will get array of your db field
i wanted to make my own quarter-final draw for the champion's league (tomorrow, friday 16 of march) : i've got 2 questions : first the modulo does not work : it shows "another match" after every entry in the array, whereas i wanted it to be written every two matches (every 2 entries)...
Second question : is there a better way to "print" the result? like a print_r without the index and where i could say "add \n after each entry" ?
<body>
<?php
$array = array("real", "barça", "bayern", "apoel", "chelsea", "milan", "benfica", "marseille" );
$new = array();
$incr = count($array);
while($incr>0){
$random = rand(0, count($array));
if (!in_array($array[$random], $new)){
$new[] = $array[$random];
if ( (count($new) % 2) ){
$new[] = " -- another match : ";
}
$incr--;
}
}
print_r($new);
?>
<p>results</p>
</body>
Thanks for your help
Another option would be to shuffle the array then just pop off each of the elements
$array = array("real", "barça", "bayern", "apoel", "chelsea", "milan", "benfica", "marseille" );
shuffle($array);
while($a = array_pop($array)) {
echo $a." vs. ".array_pop($array)." <br />";
}
Sample output:
apoel vs. real
barça vs. milan
marseille vs. bayern
chelsea vs. benfica
The modulo is working perfectly:
The array starts empty.
You add an element to it.
The length is 1, so 1 % 2, so 1, so truthy, so you add -- another match to the array
So the length is now 2
Next iteration of the loop, you add another element to the array.
The length is now 3, so 3 % 2, so 1, so truthy, so you add -- another match
And so on. Whatever it is you're trying to do, it's not what you told the server to do.
What you should probably do is something like this:
$array = Array(........);
while($a = array_shift($array)) {
$random = rand(0,count($array)-1); // -1 is important!
echo $a." vs. ".$array[$random]."<br />";
unset($array[$random)];
// no need to realign keys since array_shift already does that
}
The modulus is working exactly as you're telling it to.
(count($new) % 2) ){
when count($new) = 1, 1 % 2 = 1, = true
when count($new) = 2, 2 % 2 = 0, = false
when count($new) = 3, 3 % 2 = 1, = true
when count($new) = 4, 4 % 2 = 0, = false
when count($new) = 5, 5 % 2 = 1, = true
when count($new) = 6, 6 % 2 = 0, = false