+ 2
I need to print repeated values if there's any in php
I have an array and it's sorted with arsort In this code it prints "It's 2" but i also want to print the duplicate value and i don't know how to print them https://code.sololearn.com/wQl29r14jnzI/?ref=app
3 Answers
+ 3
You can try this
print_r("It's 2" . " ". $greaterPro);
+ 2
You can do as follows
$max_value = reset($fPro);
foreach($fPro as $key => $value) {
if($value < $max_value) break;
echo "It's $key<br>";
}
The reset function sets the internal pointer of an array to the first element and returns that first element. Since the array is already sorted that will give you the maximum value to compare in the for each loop.
Here's another version using array_filter:
$max = reset($fPro);
$farr = array_filter($fPro, function($e) use($max) {
return $e == $max;
});
print_r($e);
or in short using arrow functions:
$max = reset($fPro);
$farr = array_filter($fPro , fn($e) => $e == $max);
print_r($farr);
+ 1
I want to print the [2] position and the [3] position but not in the same line