A function to get the smaller number - php

I have 2 variables each containing a number (integer). I would like to sort them to have the lowest number first and the second largest. For example:
$sortedVar = getSmaller(45, 62); // Will return 45
$sortedVar = getSmaller(87, 23); // Will return 23
Do you see what I want to do? Can you help me please?
Thanks :)

http://php.net/manual/en/function.min.php
min — Find lowest value..
If the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.
Note:
Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an integer as though it were 0, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied.
Caution
Be careful when passing arguments with mixed types values because min() can produce unpredictable results...

Use min() which supports any number of arguments as well as arrays.
$smallest = min(1,2); //returns 1
$smallest = min(4,3,2); //returns 2
$smallest = min(array(5,4)) //returns 4

function getSmaller($a, $b) {
return $a < $b ? $a : $b;
}
In plain english, if $a is smaller than $b, then return $a, else return $b.
Or as others pointed out, there's also a function for that, called min().

$sortedVar = $a < $b ? $a : $b;

Related

What is the reason of returning -1 instead of lets say 0 at the end of this function's code?

I am talking about the second "return -1;" on the 12th line of the code. This gets reached only if two sets of numbers are exactly the same, like when comparing '192.167.11' to '192.167.11'. I will also add that using range(0,2) would be a better option for this piece of code (range(0,3) produces errors if two elements happen to be the same; I did not change that as this is the original code example from PHP Array Exercise #21 from w3resource.com).
<?php
function sort_subnets($x, $y){
$x_arr = explode('.', $x);
$y_arr = explode('.', $y);
foreach (range(0, 3) as $i) {
if ($x_arr[$i] < $y_arr[$i]) {
return -1;
} elseif ($x_arr[$i] > $y_arr[$i]) {
return 1;
}
}
return -1;
}
$subnet_list =
array('192.169.12',
'192.167.11',
'192.169.14',
'192.168.13',
'192.167.12',
'122.169.15',
'192.167.16'
);
usort($subnet_list, 'sort_subnets');
print_r($subnet_list);
?>
Returning "-1" would move the second element (the same as the first in the current $x and $y pair) towards the higher index of the array (down the array). Why not return "0" and keep everything as is if the two elements are exactly the same? Is there any reason for returning the "-1" maybe based on how the usort() works (or any other factor of this)?
Thanks.
EDIT:
I think that this is Insertion Sort (array size 6-15 elements; normally it would be Quicksort).
If the two elements are the same, there's no difference between swapping the order and keeping the order the same. So it doesn't make a difference what it returns in that case.
You're right that 0 is more appropriate. This would be more important if usort were "stable". But the documentation says
Note:
If two members compare as equal, their relative order in the sorted array is undefined.
To illustrate the excellent point of #Don'tPanic:
<?php
function sort_subnets($x, $y){
$x_arr = explode('.', $x);
$y_arr = explode('.', $y);
return $x_arr <=> $y_arr;
}
$subnet_list =
array('192.169.12',
'192.167.11',
'192.169.14',
'192.168.13',
'192.167.12',
'122.169.15',
'192.167.16'
);
usort($subnet_list, 'sort_subnets');
print_r($subnet_list);
See live code
Note the use of the "spaceship" operator, namely <=> which offers a conciseness that spares one from having to write code like the following in a function:
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
Lastly, note that the user-defined callback for usort() makes use of ternary logic because sometimes as in the case of sorting bivalent logic is insufficient. Yet, usort() itself utilizes two-part logic, returning TRUE on success and FALSE on failure.

PHP- Maintain leading 0 in number [duplicate]

I need to add numbers in php without changing the number format like below
$a = "001";
$b = "5";
$c = $a+$b;
Now the result comes like "6" but I need "006" if $a is "01" then the result should be "06".
Thanks
Technically speaking, the $a and $b in your example are strings - when you use the addition operator on them they converted to integers which can't retain leading zeroes. More details on string-to-number conversion are in the manual
Something like this would do it (assuming positive integer strings with leading zeros)
#figure out how long the result should be
$len=max(strlen($a), strlen($b));
#pad the sum to match that length
$c=str_pad($a+$b, $len, '0', STR_PAD_LEFT);
If you always know how long the string has to be, you could use sprintf, e.g.
$c=sprintf('%03d', $a+$b);
Here, % introduces a placeholder, 03 tells it we want zero padded to fill at least 3 digits, and d tells it we're formatting an integer.
Hope this would help you:
<?php
$a="001";
$b="5";
$l=max(strlen($a),strlen($b));
$c=str_pad($a+$b, $l,"0", STR_PAD_LEFT);
echo $c;
?>
For common case. Your code should looks like this.
$a = someFormat($original_a);
$b = someFormat2($original_b); // $b has different format.
$c = someFormat($a + $b);
Or, you need write formatRecognition function.
$a = getValueA();
$b = getValueB();
$c = someFormat(formatRecognition($a), $a + $b);

Combining levenshtein with in_array in PHP?

I want to verify if a levenshtein of factor <= 2 is present in an array. So:
in_array("test", $some_array);
to something like "check if in array, can have errors if levenshtein factor <= 2, by comparison"
levenshtein("test", $element_of_array_by_'in_array'_function);
Is this possible or do I have to iterate the array?
This should work for you:
You are looking for array_reduce(). With this you can reduce your array to one single return value.
You start with FALSE as return value. Then you loop through each array element and check if the return value of levenshtein() is smaller or equals 2.
If not then your return value of array_reduce() won't change and is still FALSE. If it is smaller or equals 2 you change the value to TRUE and array_reduce() will return TRUE.
array_reduce($some_array, function($keep, $v){
if(levenshtein($v, "test") <= 2)
return $keep = TRUE;
return $keep;
}, FALSE);

Sort multidimensional array with floats

I have a multidimensional array with locations data (e.g. address, phone, name,..) and their relative distance from a certain point as floats (e.g. 0.49012608405149 or 0.72952439473047 or 1.4652101344361 or 13.476735354172).
Now I need to sort this array so that it starts with the data set of closest distance (0.49012608405149) and ends with the farthest (13.476735354172).
The function I use so far does a good job, but messes up some times, which is of course as it uses strcmp
function cmp($a, $b) {
return strcmp($a["distance"], $b["distance"]);
}
usort($resultPartner, "cmp");
I googled a lot but couldn't find anything for my case. If possible I would like to avoid a foreach statement, as I read it can have a poor performance with big arrays.
Do you have any idea/experience with that and can give me a working function for this? Thank you!
strcmp() is binary safe string comparison why you don't just compare floats?
When comparing floats php manual says
Returning non-integer values from the comparison function, such as
float, will result in an internal cast to integer of the callback's
return value. So values such as 0.99 and 0.1 will both be cast to an
integer value of 0, which will compare such values as equal.
So you must be careful.
Look at this: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Since floating point calculations involve a bit of uncertainty we can
try to allow for this by seeing if two numbers are ‘close’ to each
other.
Try something like this:
function cmpfloat($a, $b) {
if (abs($a["distance"]-$b["distance"]) < 0.00000001) {
return 0; // almost equal
} else if (($a["distance"]-$b["distance"]) < 0) {
return -1;
} else {
return 1;
}
}
Following function is good if comparing integer values:
function cmp($a, $b) {
return $a["distance"] < $b["distance"] ? -1 : ($a["distance"] === $b["distance"] ? 0 : 1);
}
if a distance is smaller than b distance return -1
if a distance equals b distance return 0
if a distance is greater than b distance return 1
Reason:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Maybe in such way:
$data = array(
array('dist' => 0.72952439473047),
array('dist' => 0.49012608405149),
array('dist' => 0.95452439473047),
array('dist' => 0.12952439473047),
);
foreach ($data as $k => $v) {
$dist[$k] = $v['dist'];
}
array_multisort($dist, SORT_ASC, $data);

Adding string with number in php

$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);
Result:
23
43
I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it
It casts '3dollars' as a number, getting $a = 3.
When you echo, you add 20, to $a, so it prints 23 and $a = 23.
Then, when you print, you again add 20, so now $a = 43.
The right way to add (which is technically concatenating) strings is
$a = 7;
$b = "3 dollars";
print ($a . $b); // 73 dollars
The + operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file. Then it will show fatal error and if you didn't declare this strict then it convert string into integer.
If you need both the values, return them in an array
PHP treats '3dollars' as a integer 3 because string starting with integer and participating in arithmetic operation, so
$a = "3dollars";
$b = 20;
echo $a += $b;
it echo 23; //$a=$a+$b;
now $a = 23 + 20;
print($a += $b); //$a=$a+$b;
it print 43;
Since You have created a variable for the two, it stores the result of each, so when you added $a to 20 it will echo 23 which stores in the system, them when you print $a which is now 23 in addition to $b which is 20. You will get 43.

Categories