/    Sign up×
Community /Pin to ProfileBookmark

Max value from multi multidimensional with keys

Hello. I have simple multi array.

`
$cities = array(
0 => array(
‘City’ => ‘Los Angeles’,
‘Salary’ => ‘5000’
),
1 => array(
‘City’ => ‘Tampa’,
‘Salary’ => ‘9600’
),
2 => array(
‘City’ => ‘Sanford’,
‘Salary’ => ‘4500’
),
);
`

I know how to get the maximum salary.

`
$max = ”;
foreach($cities as $key => $value) {
$make_array[] = $value[‘Salary’];
$max = max($make_array);
}
echo $max;
`

But how do we get the maximum salary and take the city?
For example:
Salary: 9600
City: Tampa

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 19.2022 — <i>
</i>$last_sal = 0;
foreach($cities as $k=&gt;$v)
{
if($v['Salary'] &gt; $last_sal)
{
$max_ar = $v;
$last_sal = $v['Salary'];
}
}
echo "Max salary is ".$max_ar['Salary']. " in ".$max_ar['City'];
Copy linkTweet thisAlerts:
@NogDogJan 19.2022 — Not necessarily better, just more fun (I mean, come on: it uses the "spaceship" operator :) ):
[code=php]
usort($cities, function($a, $b) {
return $b['Salary'] <=> $a['Salary'];
});

echo "Max city: {$cities[0]['City']}, salary: {$cities[0]['Salary']}n";
Copy linkTweet thisAlerts:
@ginerjmJan 19.2022 — I'm just an old style coder.... I like things I can read and make sense out of with having to look it up. :)
Copy linkTweet thisAlerts:
@bumbar111authorJan 19.2022 — @ginerjm#1641710 that's exactly what I wanted
Copy linkTweet thisAlerts:
@bumbar111authorJan 24.2022 — Some correction maybe...

$last_sal = null;

the code only works with positive salary values. However, if we have negatives, it does not work.
Copy linkTweet thisAlerts:
@biberhapiJan 24.2022 — [biber hapı](https://www.biberhapiofficial.com)
Copy linkTweet thisAlerts:
@biberhapiJan 24.2022 — [lida](https://www.lidaformofficial.com)
Copy linkTweet thisAlerts:
@ginerjmJan 24.2022 — Without looking into your latest 'problem', what about setting it to 0 instead of null?
Copy linkTweet thisAlerts:
@NogDogJan 24.2022 — Maybe...
[code=php]
// leave out the initialization of $last_sal, then...
foreach($cities as $k=>$v)
{
if(!isset($last_sal) or $v['Salary'] > $last_sal)
{
$max_ar = $v;
$last_sal = $v['Salary'];
}
}
[/code]

Or, use my usort() suggestion if you don't mind changing the order of the array.

Or, if that data is coming from a DB query, add a sort on that column in the SQL so that the highest will always be first (or last if you prefer). This would probably be the most efficient solution if it would be applicable.
Copy linkTweet thisAlerts:
@ginerjmJan 24.2022 — Now that this topic has been resuscitated, I took a look and realized that I made a post earlier. So I have to wonder why the OP is suggesting changing what I gave him (where I DID set the value to 0) is being changed. NogDog and I have provided workable solutions, so why the new ideas here? Must be some other code that we are not being shown is causing this issue. Besides - why the he.. would anyone post a negative salary value in a db? And if they did any comparison against that it is not going to cause a problem that I can see.
Copy linkTweet thisAlerts:
@JerryTylerJan 25.2022 — Thank you so much for sharing this issue here.
×

Success!

Help @bumbar111 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...