max() function can't find correct value. php - php

I'm trying to get the max value in the array.
But using max() for the entire array it can't find the highest value:
The return Key is 2, which is not the correct key, obviously the highest value is in array(3).
Please help me to find my mistake.
Here bellow is my code:
<?php
$hex_splited = [
['00','00','00'],
['10', '11', '10'],
['F0', '1A', 'C3'],
['0F', 'FE', 'F4'],
];
$arr_rgb = [];
$count2 = count($hex_splited);
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
}
}
$max = max($arr_rgb);
var_dump($max);
$key = array_search($max, $arr_rgb);
echo "<pre>";
echo "array key is: " . $key;
echo "</pre>";
echo "<table border='1'>";
for ($m=0; $m < $count2; $m++) {
echo "<tr>";
for ($k=0; $k < $inn_count; $k++) {
echo "<td>";
echo $arr_rgb[$m][$k];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";

The problem is that max() doesn't always behave as you would expect when using multidimensional arrays.
This code does the processing in two stages, first finds the max value. As this is only a 2 dimensional simple array, you can use...
$max = max(array_map("max", $hex_splited));
This applies max to each of the sub arrays and then gets the max of those values.
So in your case, this will be 'FE'. Then as part of the loop which converts the data to decimal, it compares the original value with the max already found and stores the key...
$key = 0;
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
// Check if found the max value
if ( $max == $hex_splited[$i][$j] ) {
$key = $i;
}
}
}

Related

Find for each sub array in multidimensional array, if element on fixed position is highest value for this sub array

Hello all.
For each sub array in $arr_rgb (Fig.01) I have to find:
If element with $key number 1 is the highest number in this sub array, then return the $key number
of the sub array (Fig.03 and Fig.04.
So far, I can find highest number for each sub array – fig.02
In the attached image the "trouble" I have is explained.
And the code that I use so far:
<?php
$hex_splited = [
['00','00','00'],
['10', '11', '10'],
['F0', '1A', 'C3'],
['0F', 'FE', 'F4'],
];
$arr_rgb = [];
$count2 = count($hex_splited);
for ($i=0; $i < $count2; $i++) {
$inn_count = count($hex_splited[$i]);
for ($j=0; $j < $inn_count; $j++) {
$val = hexdec($hex_splited[$i][$j]);
$arr_rgb[$i][$j] = $val;
foreach ($arr_rgb as $key => $value) {
$resultMax[$key] = max($value);
}
}
}
echo "<pre>";
var_dump($resultMax);
echo "</pre>";
echo "<table border='1'>";
for ($m=0; $m < $count2; $m++) {
echo "<tr>";
for ($k=0; $k < $inn_count; $k++) {
echo "<td>";
echo $arr_rgb[$m][$k];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Determining the maximum using max only properly works for integers, so I would use array_map with hexdec to achieve that.
Then all it needs is a comparison of the value at position 1 with that maximum, and if they are the same, then the index gets added to the result array:
$result = [];
foreach($hex_splited as $key => $value) {
if(hexdec($value[1]) == max(array_map('hexdec', $value))) {
$result[] = $key;
}
}
var_dump($result);
This gets you 0, 1, 3 as result. (As IMHO it should be, according to your stated requirements - the value 00 at position 1 in the first sub-array is also the maximum of all values in that sub-array. If you need to exclude 0 values, then you’ll have to modify accordingly.)
EDIT:
if all elements are equal, then return nothing
Okay, that can also be implemented quite easily - we use array_count_values, and if the count of that is only 1, then that means all the elements had the exact same value.
So then it becomes
foreach($hex_splited as $key => $value) {
if(count(array_count_values($value)) != 1 &&
hexdec($value[1]) == max(array_map('hexdec', $value))) {
$result[] = $key;
}
}
With that, you get [1, 3] as $result here.

I have 6 values in array but when I execute, only 4 values are showed. why?

I have 6 values in array $arr1 but when I execute the code, only 4 values are shown, why?
The code finds that the maximum value is 526, but where I use echo to list the whole array the 526-valued array is not shown.
Here is the code:
<?php
$arr1[0][ ]=110;
$arr1[0][ ]=20;
$arr1[0][ ]=526;
$arr1[1][ ]=105;
$arr1[1][ ]=56;
$arr1[1][ ]=96;
echo "The given array is : <br>";
for($i=0;$i=count($arr1);$i++)
{
for($j=0;$j=count($arr1);$j++)
{
Echo "\$arr1[$i][$j] =",$arr1[$i][$j],"<br>";
}
}
$b=0;
foreach($arr1 as $val)
{
foreach($val as $key=>$val1)
{
$b=$val1;
}
}
Echo "The maximum value in the array is =",$b;
?>
There are a few issues with your code example.
The array $arr1 has 2 entries with keys 0 and 1 so if you loop 2 times 2 values, you get 4 values instead of 6.
Then per key, there are 3 entries, so for the second loop you have to use count($arr1[$i]) using [$i] to count the entries for that key.
In the loops, you are setting the value of $i to the count causing an infinite loop $i=count($arr1) while you have to use a < sign instead.
In the second part finding the largest value, you have to first check if the new value is greater than the current value, or else you will always have the last value.
$arr1[0][] = 110;
$arr1[0][] = 20;
$arr1[0][] = 526;
$arr1[1][] = 105;
$arr1[1][] = 56;
$arr1[1][] = 96;
echo "The given array is : <br>";
for ($i = 0; $i < count($arr1); $i++) {
for ($j = 0; $j < count($arr1[$i]); $j++) {
echo "\$arr1[$i][$j] =", $arr1[$i][$j], "<br>";
}
}
$b = 0;
foreach ($arr1 as $val) {
foreach ($val as $key => $val1) {
if ($val1 > $b)
$b = $val1;
}
}
echo "The maximum value in the array is =", $b;
Output
The given array is :
$arr1[0][0] =110
$arr1[0][1] =20
$arr1[0][2] =526
$arr1[1][0] =105
$arr1[1][1] =56
$arr1[1][2] =96
The maximum value in the array is =526
See a Php demo
As #Nick points out, you could also compute the max in the nested for loops directly:
$b = 0;
echo "The given array is :" . PHP_EOL;
for ($i = 0; $i < count($arr1); $i++) {
for ($j = 0; $j < count($arr1[$i]); $j++) {
if ($arr1[$i][$j] > $b) $b = $arr1[$i][$j];
echo "\$arr1[$i][$j] =", $arr1[$i][$j] . PHP_EOL;
}
}
Php demo
You can flatten the array by using splat operator then use max.
echo max(array_merge(...$arr1));
If you want you can use implode to print the values
echo implode(' ',array_merge(...$arr1));
DEMO :- https://3v4l.org/0ZamG
Please use this code. I hope it will works for you.
$arr1[0][ ]=110;
$arr1[0][ ]=20;
$arr1[0][ ]=526;
$arr1[1][ ]=105;
$arr1[1][ ]=56;
$arr1[1][ ]=96;
echo "The given array is : <br>";
for($i=0;$i<count($arr1);$i++)
{
for($j=0;$j<count($arr1);$j++)
{
echo "$arr1[$i][$j] =",$arr1[$i][$j],"<br>";
}
}
$b=0;
sort($arr1);
foreach($arr1 as $val)
{
foreach($val as $key=>$val1)
{
$b=$val1;
}
}
echo "The maximum value in the array is =",$b;

How do I add together Random Integers from a for loop with PHP

I was messing around with PHP and I figured out how to list 5 random integers between 1 and 100 using a for loop:
for($row = 1; $row <= 5; $row++) {
echo rand(1,100) . "<br>";
}
I know that I can get the sum of random numbers by doing the following:
$sum = 0;
for($row = 1; $row <= 5; $row++) {
$sum += rand (1,100) . "<br>";
}
echo $sum;
But now I don't know how I can echo the rands to be seen as well.
I want to be able to somehow combine the first piece of code and the second to get the list of random integers and their sum.
If you're planning to reuse the data, store them in a variable.
You should be looking into array's here:
for($sum = 0, $row = 1; $row <= 5; $row++) {
$num = rand (1,100);
$nums[] = $num;
$sum += $num;
}
echo "the total sum is: '$sum' with the values of: '" . implode(', ', $nums) . "'.";
Or just loop through it:
foreach($nums as $value){
echo $value, '<br>';
}
This is what I ended up using, and it worked! Thanks!
<?php
for($row = 1; $row <= 5; $row++) {
$value = rand (1,100);
echo $value . "<br>";
$sum += $value;
}
echo $sum;
?>

PHP get min and max values from 50 random numbers?

<?php
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand (1, 200);
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Here is code but my question is how can I find max and min from values of the table? Do I have to make random numbers somehow into array?
You can do as following without changing much of your code
<?php
$array = array(); // <-- added code
echo "<table border='1'><br />";
for ($row = 0; $row < 10; $row ++) {
echo "<tr>";
for ($col = 0; $col < 5; $col ++) {
$rand = rand(1, 200);
$array[] = $rand; // <-- added code
echo "<td>", $rand, "</td>";
}
echo "</tr>";
}
echo "</table>";
$min = min($array); // <--- added code
$max = max($array); // <--- added code
?>
Check it out the max and min value of an aray in PHP using library function.
for($col = 0; $col < 5; $col ++) {
$rand[] = rand (1, 200);
}
echo $max = max($rand)."<br/>";
echo $min = min($rand)."<br/>";
print_r($rand);

PHP Loop: Every 4 Iterations Starting From the 2nd

I have an array that can have any number of items inside it, and I need to grab the values from them at a certain pattern.
It's quite hard to explain my exact problem, but here is the kind of pattern I need to grab the values:
No
Yes
No
No
No
Yes
No
No
No
Yes
No
No
I have the following foreach() loop which is similar to what I need:
$count = 1;
foreach($_POST['input_7'] as $val) {
if ($count % 2 == 0) {
echo $val;
echo '<br>';
}
$count ++;
}
However, this will only pick up on the array items that are 'even', not in the kind of pattern that I need exactly.
Is it possible for me to amend my loop to match that what I need?
You can do this much simpler with a for loop where you set the start to 1 (the second value) and add 4 after each iteration:
for ($i = 1; $i < count($_POST['input_7']); $i += 4) {
echo $_POST['input_7'][$i] . '<br />';
}
Example:
<?php
$array = array(
'foo1', 'foo2', 'foo3', 'foo4', 'foo5',
'foo6', 'foo7', 'foo8', 'foo9', 'foo10',
'foo11', 'foo12', 'foo13', 'foo14', 'foo15'
);
for ($i = 1; $i < count($array); $i += 4) {
echo $array[$i] . '<br />';
}
?>
Output:
foo2foo6foo10foo14
DEMO
Try this:
$count = 3;
foreach($_POST['input_7'] as $val) {
if ($count % 4 == 0) {
echo $val;
echo '<br>';
}
$count ++;
}

Categories