Solved

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

Posted on 2016-10-21
14
59 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 54

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 54

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 54

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 54

Expert Comment

by:Julian Hansen
ID: 41855119
No apology necessary :)
0
 
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 109

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 54

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 109

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 54

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

813 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

13 Experts available now in Live!

Get 1:1 Help Now