Solved

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

Posted on 2016-10-21
14
45 Views
Last Modified: 2016-10-22
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

0
Comment
Question by:smksa
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 350 total points
ID: 41854862
Assume array is called $data
$min = 150;
$server = '';
foreach($data as $i) {
  if ($i[2] < $min) {
        $min = $i[2];
        $server = $i[0];
  }
}
if (!empty($server)) {
  echo "Server with lowest value [{$min}] is [{$server}]<br/>";
}
else {
   echo "No servers with value < 150 found<br/>";
}

Open in new window

0
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 150 total points
ID: 41854924
You can sort your array by the server value in ascending order and then check the first value, if it's > 150 means that there're not server with value < 150 otherwise the first value in the list is the lowest

So, with a function like this you can get the server with the lowest value
function GetServer($servers)
{
	$server = array();
	foreach ($servers as $key => $row)
	{
    	$server[$key] = $row[2];
	}
	array_multisort($server, SORT_ASC, $servers);
	if ($servers[0][2] < 150)
		{ 
			return $servers[0][0].': '.$servers[0][2];
		}
	else
		{
			return 'No servers with value less than 150';
		}
}
example call
echo GetServer($servers);

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41854954
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

1
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41855100
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.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 41855111
@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
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41855117
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.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41855119
No apology necessary :)
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:skullnobrains
ID: 41855150
$min=1000000;

foreach( $your_array as $s )
  $min < min($min,$s[2])
  and $minserverinfo=$s;
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41855161
@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
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 41855176
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.
1
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41855179
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?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41855258
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.
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41855277
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.
0
 
LVL 2

Author Closing Comment

by:smksa
ID: 41855345
Thanks to all of you !
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now