+ 4
Working with arrays
suppose i have a multi-dimensional array $location = [loc1=>10,loc2=>20,loc3=>5]; how would i go about checking the array to find the least value...suppose in php , Thanks
7 Answers
+ 1
https://code.sololearn.com/wCP6mFCayab7/#php
<?php
$locations = array
(
'loc1' => 10,
'loc2' => 20,
'loc3' => 5
);
function get_max_value($locations)
{
$key_of_max_value_so_far = array_keys($locations)[0];
$max_value_so_far = $locations[$key_of_max_value_so_far];
foreach ($locations as $key => $value)
{
if ($value > $max_value_so_far)
{
$max_value_so_far = $value;
$key_of_max_value_so_far = $key;
}
}
return array($key_of_max_value_so_far => $max_value_so_far);
}
print_r(get_max_value($locations));
?>
OUTPUT of above code:
Array ( [loc2] => 20 )
+ 1
i appreciate @Xan but i have one problem... i got lost reading your code lol, if u dont mind can u help illustrate your code in human understandable language lol like w3schools..
Esp at the beggining of the get_max_value() , can u tell me what the code is doing at $key_of_max_value_so_far & $max_value_so_far ..
thanks :)
+ 1
Sure :-)
+ 1
thanks alot again.. im still new to the concept of sort n algorithms but im learning, can u suggest a good alternative resource to learn Data Structures & Algorithm (begginer and intermediate).I'll be learning it at school in the near future but i prefer self learning plus i hate suprises lol...
i hope u feel better by knowing u have added me some knowledge :)
i was wondering how to impliment this so i can use it in a project that uses Maps Api to get distance between multiple points which are returned as an array... so its gonna be useful for calculating the shortest distance in relation :)
+ 1
I don't know any learning resources without Googling. I first learned all this stuff, pre-internet! We learned from books and each other. I'm therefore out of touch with learning resources for people starting their journey.
I'm not sure which maps API you're referring to, however the steps are:
1) Find their method that calculates distances between places. If that doesn't exist, then just use Pythagorus to calculate the diagonal
2) Then populate an array to fit the code I gave you.
0
I've made the code simpler, and I've now added comments. Is that a bit better? If not, let me know, and I'll try further to explain. I appreciate the feedback :-)
https://code.sololearn.com/wCP6mFCayab7/#php
<?php
$locations = array
(
// location (which is a key) => value.
'loc1' => 10,
'loc2' => 20,
'loc3' => 5
);
function get_max_value($locations)
{
/**
* Reverse sort the array (by value, not by the key).
* i.e. we sort using the numeric values in our case (10, 20, 5) etc.
* arsort, stands for Associative (array) Reverse Sort.
* The $locations array is an associative array.
* This type of sort conserves the keys ('loc1' etc).
*/
arsort($locations);
/**
* After the reverse sort the array will look like this:
* $locations = array
* (
* 'loc2' => 20,
* 'loc1' => 10,
* 'loc3' => 5
* )
*/
/**
* After the reverse sort, the highest value will be at the start
* of the array.
* So we just need to get the first location / value pair from the array.
*/
$max_value = reset($locations);
$max_location = key($locations);
// Return the location / value pair.
return array($max_location => $max_value);
}
print_r(get_max_value($locations));
?>
OUTPUT of above code:
Array ( [loc2] => 20 )
0
sorry guys.. i asked too soon, i was talking about Google Maps Api, turns out when youre using Direction matrix it does it all for you
i.e. you can feed it multiple destination addresses and origin address