Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating an incrementing number starting with 1001. If the number goes 1001,1002,1003... when it reaches 10, will it be formatted like 1010 or will it be 10010? I need it to just go in order and be 1010 and when it reaches 100, 1100.
$prefix = "1"; // update the prefix here
$number = 1;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique);
When your count reaches 10, the number printed will be 1010. As described here, str_pad "Pads a string to a certain length with another string" You can create a test with the following:
$prefix = "1"; // update the prefix here
$number = 1;
for ($number = 1; $number <= 100; $number++)
{
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print($unique."\n");
}
When your count reaches 100, the number printed will be 1100.
However, if you were to go up to 1000, 11000 would be printed - str_pad apparently will not truncate the string to match the specified size.
It will be 1010, but you can test this yourself easily:
$prefix = "1"; // update the prefix here
$number = 9;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique); // 1010
The second argument of str_pad specifies padding. If padding is 3, then 1 becomes 001, 10 becomes 010, 100 becomes 100.
With your code it will be 10010.
It looks like you are making this more complicated than it needs to be. Why not just start with $number = 1001 and increment it and then turn it into a string?
$number = 1001;
$number++;
$unique = strval($number);
print_r($unique);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have strings: 23-65, 123-45, 2-5435, 345-4
I want to add zeros to them so all of them will look like ###-#### (three digits dash four digits): 023-0065, 123-0045, 002-5435, 345-0004
How can i do it in php?
Thanks!
You will need to split them using
$parts = explode('-', $number);`
then use str_pad function:
$parts[0] = str_pad($parts[0], 3, "0");
$parts[1] = str_pad($parts[0], 4, "0");
and then concatenate them again
$number = implode('-', $parts);
Alternatively you can pad them using vsprintf:
$number = vsprintf('%03d-%04d', $parts);
Try:
$str = "23-65, 123-45, 2-5435, 345-4";
$numArray = explode(",",$str);
$str_new = "";
foreach($numArray as $nums) {
$nums = explode("-",$nums);
$num1 = str_pad($nums[0], 3, '0', STR_PAD_LEFT);
$num2 = str_pad($nums[1], 4, '0', STR_PAD_LEFT);
$str_new .= $num1."-".$num2.",";
}
$str_new = rtrim($str_new,",");
Output:
023-0065, 123-0045,0 2-5435, 345-0004
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
$a = array(2, 6, 24, 16, 7, 10);
I know how to add all the numbers using array_sum() but if I want to add only the numbers between 2 and 16 how can do that?
This is one of the solution that that I've come up with:
$a = array(2, 6, 24, 16, 7, 10);
$r = array_slice($a, 0, -2);
print_r (array_sum($r));
Just want to know if there is any other way to get the result.
To deal with dynamic bound limits you can extend the initial approach(array_slice + array_sum) using array_search function:
$arr = [2, 6, 24, 16, 7, 10];
$a = 10;
$b = 24;
$lowerBound = array_search($a, $arr);
$upperBound = array_search($b, $arr);
if (($low = $lowerBound) > $upperBound) { // if bounds were confused
$lowerBound = $upperBound;
$upperBound = $low;
}
$sum = array_sum(array_slice($arr, $lowerBound, $upperBound - $lowerBound + 1));
print_r($sum); // 57
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone show me how I would write a php function that converts a base 36 string to a base 10 integer without using the base convert function
the function should work like this
echo base36_to_base10('614qa'); //prints 10130482
echo base36_to_base10('614z1'); //prints 10130797
Just use the native base_convert function:
echo base_convert('614qa', 36, 10);
or if you prefer:
function base36to10($value) {
return base_convert($value, 36, 10);
}
If you can'r or won't use base_convert, this should do it:
function base36to10($value) {
// check for correct input
if (preg_match('/^[0-9A-Z]+$/i', $value) == 0) {
return NULL;
}
// reverse and change to uppercase
$value = strtoupper(strrev($value));
// converted value
$converted = 0;
// cycle on character
for ($c = 0, $l = strlen($value); $c < $l; ++$c) {
// if the character is a digit
if (ctype_digit($value[$c])) {
// convert directly
$v = (int) $value[$c];
}
// else convert ascii value
else {
$v = ord($value[$c]) - 55; // -55 == 10 - 65
}
// add to converted
$converted += $v * pow(36, $c);
}
// now return
return $converted;
}
Slightly different option using a array of symbols:
function base36_to_base10($input) {
$symbols = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
,'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
//flip the array so symbols are keys (or just write it that way to begin with)
$symbols = array_flip($symbols);
// reverse input string and convert to array
// (reversing the string simplifies incrementing place value as you iterate it)
$x = str_split(strrev($input));
$sum = 0;
foreach ($x as $place => $symbol) {
// increment sum with base 10 representation of base 36 place value
$sum += $symbols[$symbol] * pow(36, $place);
// or with PHP 5.6+
//$sum += $symbols[$symbol] * 36 ** $place;
}
return $sum;
}
How it works with one of your examples:
reverse input = 614qa -> aq416
initialize sum = 0
a -> 10, 10 * 36^0 = 10, sum + 10 = 10
q -> 26, 26 * 36^1 = 936, sum + 936 = 946
4 -> 4, 4 * 36^2 = 5184, sum + 5184 = 6130
1 -> 1, 1 * 36^3 = 46656, sum + 46656 = 52786
6 -> 6, 6 * 36^4 = 10077696, sum + 10077696 = 10130482
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Want to divide random integer number into equal five parts. And insert those values into array. Can any one tell me the logic for that.
Eg. I have 15 as my number. After dividing. It should generate the array as below.
$myArray = array('3','6','9','12','15');
Thanks in advance.
function getParts($number, $parts)
{
return array_map('round', array_slice(range(0, $number, $number / $parts), 1));
}
print_r(getParts(15, 5));
Explanation: range() generates the array of values starting with 0, ending when it reaches $number and using the step $number/$parts. It will get $parts+1 floating point numbers. array_slice() removes the first item (which is always 0). array_map() applies the function round() to each element to get the nearest integer.
Divide and create a loop to fill the array...
$total = 15;
$divide = 5;
$base = $total / $divide;
$arr = array();
for($i = 1; $i <= $divide; $i++) {
$arr[] = round($i * $base);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am testing a php function i found on the php documentation based on this comment:
http://www.php.net/manual/en/function.rand.php#108861
<?php
function RandNumber($e) {
for ($i = 0; $i < $e; $i++) {
$rand = $rand . rand(0, 9);
}
return $rand;
}
echo RandNumber(4);
// Outputs a 6 digit random number
?>
I get the error Notice:
Undefined variable: rand in /var/www/eod.php on line 7
This is line 7:
$rand = $rand . rand(0, 9);
Why is causing this error since the function works as expected?
Initialize your $rand variable to remove this warning :
function RandNumber($e){
$rand="";
for($i=0;$i<$e;$i++){
$rand = $rand.rand(0, 9);
}
return $rand;
}
Note that you may simplify your code :
function RandNumber($e){
$rand="";
for(;$e-->0;){ // no need for an additional variable
$rand .= rand(0, 9); // addition and assignement with one operator
}
return $rand;
}
To output a random 6 digit number (If you need larger numbers, greater than mt_getrandmax(), this solution would fail), simply use
mt_rand(100000, 999999);
To include numbers with leading zeros (000123), you can use a combination of my_rand(0, 999999) and str_pad($number, 6, "0", STR_PAD_LEFT);
No need for fancy looping.
This is not an error.. it's a notice message.. If you don't want to appear that notice you should define it before doing the loop:
$rand = '';
for($i=0;$i<$e;$i++){
$rand = $rand . rand(0, 9);
}