Link to home
Start Free TrialLog in
Avatar of Shamsul Kamal
Shamsul Kamal

asked on

Pick the lowest value in between value from the array in PHP ?

HI,

I have the array as attached, may i know how to pickup the server name with the lowest value in it ? But the server name value must not exceed 150 ? i just want the servername become variable, so that when i called $servername this server confirm to have the lowest value in it.

Array
(
    [0] => Array
        (
            [0] => Server25
            [1] => cpanel11
            [2] => 145
        )

    [1] => Array
        (
            [0] => Server16
            [1] => cpanel11
            [2] => 127
        )

    [2] => Array
        (
            [0] => Server20
            [1] => cpanel11
            [2] => 145
        )

    [3] => Array
        (
            [0] => Server22
            [1] => cpanel11
            [2] => 162
        )

    [4] => Array
        (
            [0] => Server24
            [1] => cpanel11
            [2] => 146
        )

    [5] => Array
        (
            [0] => Server26
            [1] => cpanel11
            [2] => 144
        )

    [6] => Array
        (
            [0] => Server28
            [1] => cpanel11
            [2] => 145
        )

    [7] => Array
        (
            [0] => Server31
            [1] => cpanel11
            [2] => 143
        )

    [8] => Array
        (
            [0] => Server32
            [1] => cpanel11
            [2] => 142
        )

    [9] => Array
        (
            [0] => Server33
            [1] => cpanel11
            [2] => 138
        )

    [10] => Array
        (
            [0] => Server37
            [1] => cpanel11
            [2] => 143
        )

    [11] => Array
        (
            [0] => Server34
            [1] => cpanel11
            [2] => 139
        )

    [12] => Array
        (
            [0] => Server35
            [1] => cpanel11
            [2] => 150
        )

    [13] => Array
        (
            [0] => Server41
            [1] => cpanel11
            [2] => 140
        )

    [14] => Array
        (
            [0] => Server39
            [1] => cpanel11
            [2] => 146
        )

    [15] => Array
        (
            [0] => Server45
            [1] => cpanel11
            [2] => 149
        )

    [16] => Array
        (
            [0] => Server42
            [1] => cpanel11
            [2] => 94
        )

    [17] => Array
        (
            [0] => Server43
            [1] => cpanel11
            [2] => 142
        )

    [18] => Array
        (
            [0] => Server44
            [1] => cpanel11
            [2] => 143
        )

    [19] => Array
        (
            [0] => Server47
            [1] => cpanel11
            [2] => 106
        )

    [20] => Array
        (
            [0] => Server48
            [1] => cpanel11
            [2] => 151
        )

    [21] => Array
        (
            [0] => Server51
            [1] => cpanel11
            [2] => 131
        )

    [22] => Array
        (
            [0] => Server52
            [1] => cpanel11
            [2] => 139
        )

    [23] => Array
        (
            [0] => Mara1
            [1] => cpanel11
            [2] => 115
        )

    [24] => Array
        (
            [0] => Server53
            [1] => cpanel11
            [2] => 153
        )

    [25] => Array
        (
            [0] => LinuxPro1
            [1] => cpanel11
            [2] => 120
        )

    [26] => Array
        (
            [0] => LinuxPro2
            [1] => cpanel11
            [2] => 134
        )

)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you are going to do a pass through the array so you can create another array which you are then going to sort - why not just check for the value on the first iteration?
foreach ($servers as $key => $row)
{
    	$server[$key] = $row[2];
}

Open in new window

He's asking for the lowest value, not the first less than 150, so first I sort the multi dimensional array using array_multisort to order in ascending by server value.
@Julian
Uhm, sorry, I've misread your code and lost the assignment to $min if lowest.
yes you're right, your code does almost the same than mine but should be more efficient as it doesn't call any additional sort. I apologize
Yes but if you are going to iterate through the array to add its elements to another array which is then going to be sorted -  you might as well makes use of the first iteration to check for the lowest value which eliminates the need to sort - otherwise you are unnecessarily spining the CPU's wheels for no extra gain

In your first loop
foreach ($servers as $key => $row)
{
  if ($row[2] < $min) {
     // check for min here
  }
  ...
}
// check if min value < 150

Open in new window


The above method is about 2.5-3 times faster as it eliminates the extra step of sorting.
No apology necessary :)
Avatar of skullnobrains
skullnobrains

$min=1000000;

foreach( $your_array as $s )
  $min < min($min,$s[2])
  and $minserverinfo=$s;
@smksa: No points for this, please, but this is something that cries out for a database or object-oriented notation.  You might think about organizing the little "sub-arrays" into objects, then injecting the objects into the collection of the larger array.  A design like this would facilitate sorting because the objects would have named properties.  You could do this with associative arrays, too, but objects give you the greater flexibility to add methods to the data, and arrays don't offer that.

And don't get hung up on performance issues.  Premature optimization is a recognized anti-practice, so we avoid it.  Until you can demonstrate a performance problem it's not worth asking if one way is faster than another -- we can easily lose hours trying to optimize something that runs in microseconds!

Best of luck with your project, ~Ray
following @ray's first idea, using the number as the array's key when generating the initial array seems meaningful. but php arrays are not sorted by key so you'd still need a sort at some point

knowing more about your project might help use direct you towards efficiency

Premature optimization is a recognized anti-practice, so we avoid it.

 i have to disagree on that one : there are cases when you would not care but most of the time optimisations are VERY important mainly at the beginning of projects when you create libs that will be reused.

additionally i believe that an algorithm that is less efficient is less easy to read and maintain. if i see a array_sort(), i'm assuming that more than one element has to be used in a specific order later on and i will loose time and some hair figuring out that the coder did this just in order to pick the smallest value. in my opinion useless code that runs is much worse than dead code that does not.
Sorting is not a requirement here.

Sorting is at least an O(N) O(N log N) on average.

A single pass to find the smallest value is always O(N)

Can't see the logic behind sorting the array?
Depending on the data structure, there is this:
http://php.net/manual/en/function.min.php

Also, I don't think of optimized code and premature  optimization as being the same things.  Using common-sense best practices for best performance is always a good idea.  Getting distracted by things like what kind of quote marks, or whether it's faster to use one kind of key or another in a small data set is the "premature" version of optimization, and that's what I regard as a time-waster that is to be avoided.
There is a tendency of late toward lazy programming. This is a classic case in point. It might appear quick and easy to sort the array and then pop of the first (or last depending on the sort) - but if you are using this approach it is probably because you have not thought about the problem and if you have not thought about the problem then there is a good chance your code is going to have other logic bombs lying in wait.

These little "shortcuts" add up not only in terms of performance but in terms of badly designed code because the person writing the code has not given enough thought to the problem.

This is all fluff anyway because most likely in a real situation the data would be in a database and the correct way of retrieving the results would be with a query. However, in the context of the question asked an answer or approach that is predicated on lazy thinking (irrespective of how few microseconds it saves) is not an answer.
Avatar of Shamsul Kamal

ASKER

Thanks to all of you !