php min max function not working properly - php

I have code to get minimum and maximum value from comma separated value ranges by using below given code
<?php
$price=$_GET['price'];
$grade = str_replace('-', ',', $price);
$number = array($grade);
$max = max($number);
$min = min($number);
echo "min value is $min <br/>";
echo "max value is $max <br/>";
?>
for the input ?price=0-5,4-30,6-50 This should output minimum value 0 and maximum value 50 but my above code is giving output as
min value is 0,5,4,30,6,50
max value is 0,5,4,30,6,50
Kindly guide me where i am making mistake or any other working alternate.

You are incorrect with creating an array. Please use explode for this. Explode function will break the string into array.
First parameter is the character on which you want to split the string and the second one is input. In your case it would be nice to add the str_replace function right there, so you don't change the original input.
$input = '0-5,4-30,6-50';
$numbers = explode(',', str_replace('-', ',', $input));
And now you can use min and max functions and they will work properly.

After using str_replace method, you are converting a string into an array, your array looks like:
Array ( [0] => 0,5,4,30,6,50 )
With this array, you cant achieve or get the maximum and minimum value from an array.
You need to explode your string with comma as:
$yourArr = explode(",", $grade); // this will convert string into array.
Now your result should looks like:
Array ( [0] => 0 [1] => 5 [2] => 4 [3] => 30 [4] => 6 [5] => 50 )
Complete Example:
<?php
$price='0-5,4-30,6-50';
$grade = str_replace('-', ',', $price);
$yourArr = explode(",", $grade);
$max = max($yourArr);
$min = min($yourArr);
echo "min value is $min <br/>";
echo "max value is $max <br/>";
?>
Result:
min value is 0
max value is 50

<?php
$price = '0-5,4-30,6-50';
if(preg_match_all('/\d+/', $price, $matches)) {
$min = min($matches[0]);
$max = max($matches[0]);
var_dump($min, $max);
}
Output:
string(1) "0"
string(2) "50"

Related

sort() Multidimensional Arrays with change value before

I want sorting multidimensional array. But I must to change format value to sorting before then get back format in beginning.
This my array (multidimensional)
$db = [['1','00:01:13.145'], ['2','00:02:19.145'],
['3','00:02:13.235'], ['4','00:01:44.020'],
['5','00:02:25.035'], ['6','00:01:11.031']];
For example can help you to answer, I Know a part how to sorting array time and to get format back again:
This my array (single)
$db2 = ['00:01:13.145','00:02:19.145',
'00:02:13.235','00:01:44.020',
'00:02:25.035','00:01:11.031'];
Function to sorting this
$rep = str_replace(':','', str_replace('.','', $db2));
echo arr($rep);
function arr($array) {
$return = array();
foreach($array as $row) {
$return[] = (int)$row;
sort($return);
}
return $return;
}
The output will be
Array ( [0] => 111031 [1] => 113145 [2] => 144020 [3] => 213235 [4] => 219145 [5] => 225035 )
Second, I know how to get format again, example:
$int = 111031;
$str = substr_replace(substr_replace($int,".",-3,-3),":",-6,-6);
echo substr_replace($str,'00:0',-9,-9);
from (int)111031 the output will be 00:01:11.031
Pleas help me to solve this
This should work for you:
Here we simply use usort() to use our own custom compare function. In this function we first explode() the time by a dot, so we get the time and the milliseconds separately. After this we convert the time into a timestamp with strtotime() and multiply it by 1,000 so we can add the milliseconds to it.
With this we have converted the time into milliseconds and we can simply compare the numbers and sort by the milliseconds.
Code:
<?php
$db = [
['1','00:01:13.145'], ['2','00:02:19.145'],
['3','00:02:13.235'], ['4','00:01:44.020'],
['5','00:02:25.035'], ['6','00:01:11.031']
];
usort($db, function($a, $b){
list($timeOne, $millisecondsOne) = explode(".", $a[1]);
list($timeTwo, $millisecondsTwo) = explode(".", $b[1]);
$millisecondsOne = strtotime($timeOne) * 1000 + $millisecondsOne;
$millisecondsTwo = strtotime($timeTwo) * 1000 + $millisecondsTwo;
if($millisecondsOne == $millisecondsTwo)
return 0;
return $millisecondsOne > $millisecondsTwo ? 1 : -1;
});
print_r($db);
?>

PHP Array of Price Ranges - Retrieve the lowest and highest

I'm working with a PHP array that shows a range of prices, where an individual item is in the form of "price from-price to". Here's an example of the array:
Array
(
[0] => $625,000-$700,000
[1] => $550,000-$625,000
[2] => $1,000,000-$1,250,000
[3] => $925,000-$1,000,000
)
I now need to retrieve the lowest and highest price in the array, however as they are a range I first need to pull apart each price in each array item. For example using the above array I would like to be able to return something like this:
$minPrice = 550000;
$maxPrice = 1250000;
I'm new to PHP and completely stumped at this point about how to go about parsing each of the values from each array item then getting the lowest/highest value.
I would put all the normalized prices in an array and use the php max, min functions.
<?php
$ranges = array(
"$625,000-$700,000",
"$550,000-$625,000",
"$1,000,000-$1,250,000",
"$925,000-$1,000,000",
);
$prices = array();
foreach ($ranges as $range) {
$prices = array_merge($prices, explode("-", preg_replace("/[\\$,]/i", "", $range)));
}
$maxPrice = max($prices);
$minPrice = min($prices);
try this:
$array = Array
(
0 => '$625,000-$700,000',
1 => '$550,000-$625,000',
2 => '$1,000,000-$1,250,000',
3 => '$925,000-$1,000,000',
);
$tmp = array();
foreach($array as $key=>$value){
$cleanValue = explode('-', str_replace(array('$',','), array('',''), $value));
foreach($cleanValue as $k=>$v){
$tmp[] = $v;
}
}
$minValue = min($tmp);
$maxValue = max($tmp);
echo $minValue . ' '. $maxValue;
Please refer code below.
First value are exploded to an array and then "$" is removed and then value is converted to integer. Then values are added to array and finally min / max functions are called.
$maxPrice = 0;
$max_curr = 0;
$price_arr = array();
foreach($min_maxs as $min_max){
$min_max_arr = explode("-",$min_max); // string to array
array_walk($min_max_arr,"remove_dollar_commas"); // remove "$"
$price_arr = array_merge($price_arr , $min_max_arr ); // add to final array
}
$maxPrice = max($price_arr); // call max function to get maximum value
$minPrice = min($price_arr); // call min function to get minimum value
function remove_dollar_commas(&$subject){
$subject = intval( str_replace(array("\$",","), "", $subject) );
}

Put number separately in array

I would like to put a number separately in an array
ex.
$num = 345;
should be in an array so i can call the numbers as
$num[1] (which should return 4)
I tried str_split($num,1) but without succes.
Thanks
EDIT -------
After some more research str_split($num,1) actually did the trick.
(thanks, Crayon Violent)
$num = 345;
$arr1 = str_split($num); print_r($arr1); //Array ( [0] => 3 1 => 4
[2] => 5 )
echo $arr11; //4
str-split
If you are just trying to get individual characters from the string, use substr.
$second_digit = substr( $num, 1, 1 );
while ($num >0) {
$arr[i] = $num %10;
$num = $num/10;
i++
}
//this leaves the array in reverse order i.e. 543
//to flip
array_reverse($arr);

Need Help in building a logic

I need to get all the positions of a character in a string in a form of an array. I know about the php function strpos() but it does not accept an array as an argument.
This is required:
$name = "australia"; //string that needs to be searched
$positions_to_find_for = "a"; // Find all positions of character "a" in an array
$positions_array = [0,5,8]; // This should be the output that says character "a" comes at positions 0, 5 and 8 in string "australia"
Question: What Loops can help me build a function that can help me achieve the required output?
You can use a for to loop that string:
$name = "australia";
$container = array();
$search = 'a';
for($i=0; $i<strlen($name); $i++){
if($name[$i] == $search) $container[] = $i;
}
print_r($container);
/*
Array
(
[0] => 0
[1] => 5
[2] => 8
)
*/
Codepad Example
No loops necessary
$str = 'australia';
$letter='a';
$letterPositions = array_keys(
array_intersect(
str_split($str),
array($letter)
)
);
var_dump($letterPositions);

How to replace number data within a string to a different number?

There is a string variable containing number data , say $x = "OP/99/DIR"; . The position of the number data may change at any circumstance by user desire by modifying it inside the application , and the slash bar may be changed by any other character ; but the number data is mandatory. How to replace the number data to a different number ? example OP/99/DIR is changed to OP/100/DIR.
$string="OP/99/DIR";
$replace_number=100;
$string = preg_replace('!\d+!', $replace_number, $string);
print $string;
Output:
OP/100/DIR
Assuming the number only occurs once:
$content = str_replace($originalText, $numberToReplace, $numberToReplaceWith);
To change the first occurance only:
$content = str_replace($originalText, $numberToReplace, $numberToReplaceWith, 1);
Using regex and preg_replace
$x="OP/99/DIR";
$new = 100;
$x=preg_replace('/\d+/e','$new',$x);
print $x;
The most flexible solution is to use preg_replace_callback() so you can do whatever you want with the matches. This matches a single number in the string and then replaces it for the number plus one.
root#xxx:~# more test.php
<?php
function callback($matches) {
//If there's another match, do something, if invalid
return $matches[0] + 1;
}
$d[] = "OP/9/DIR";
$d[] = "9\$OP\$DIR";
$d[] = "DIR%OP%9";
$d[] = "OP/9321/DIR";
$d[] = "9321\$OP\$DIR";
$d[] = "DIR%OP%9321";
//Change regexp to use the proper separator if needed
$d2 = preg_replace_callback("(\d+)","callback",$d);
print_r($d2);
?>
root#xxx:~# php test.php
Array
(
[0] => OP/10/DIR
[1] => 10$OP$DIR
[2] => DIR%OP%10
[3] => OP/9322/DIR
[4] => 9322$OP$DIR
[5] => DIR%OP%9322
)

Categories