0

Hi all,

I have a multidimensional array [0]:[0], [1], [2], [3] and I want to try and find the min and max values of each index of that array over the whole array. I am using:

for ($i = 0; $i < count($input); $i++)
{
    $string1[] = $input[$i][1];
    $string2[] = $input[$i][2];
    $string3[] = $input[$i][3];
    $string4[] = $input[$i][4];
}

This gives me a linear array of all the values that I then pass into:

    $string1_max = max($string1);
    $string1_min = min($string1);
    $x_ys1 = nValues($string1_min, $string1_max, 0, 1);

    $string2_max = max($string2);
    $string2_min = min($string2);
    $x_ys2 = nValues($string2_min, $string2_max, 0, 1);

    $string3_max = max($string3);
    $string3_min = min($string3);
    $x_ys3 = nValues($string3_min, $string3_max, 0, 1);

    $string4_max = max($string4);
    $string4_min = min($string4);
    $x_ys4 = nValues($string4_min, $string4_max, 0, 1);

My question is: is there a way to code this formular when the indexes within the multidimensional array are unknown? At the moment they are hard coded from 1-4... but I want to be able to code them 1-x (0 is missed on purpose).

I tried playing around with multiple for statements, but just got confused. Is there a way to do this?

3
Contributors
3
Replies
17
Views
1 Year
Discussion Span
Last Post by James_43
0

If I understood your question , the answer is quite simple:

$arr =  [
            [21,43,54,34],
            [2.5,53.2,24,55],
            [32,11,2,5]
        ];
$minArr = [];

for( $i=0; $i < count($arr); $i++)
{
    $minArr[$i] = min($arr[$i]);
}

Edited by jkon

0

If u want max as well

As jkon but with...

$minmaxArr = [];
for( $i=0; $i < count($arr); $i++)
{
    $minmaxArr[$i] = [min($arr[$i]),max($arr[$i])];
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.