/    Sign up×
Community /Pin to ProfileBookmark

Compare the value from and ascending

I have array

[CODE]Array
(
[node] => description
[level] => 5
)
Array
(
[node] => book
[level] => 4
)
Array
(
[node] => hi
[level] => 5
)
Array
(
[node] => room
[level] => 3
)
Array
(
[node] => library
[level] => 2
)
[/CODE]

in is $vysledek4 from
foreach ($vysledek3 as $vysledek4)
{}
and i want some thing like that

[level] => 2
[node] => library

[level] => 3
[node] => room

[level] => 4
[node] => book

[node] => hi
[level] => 5
it will be output ascending as level
i tried

[CODE]if(!empty($vysledek4[‘level’]))
{
$i = 2;
while ($i<5)
{
if($i==$vysledek4[‘level’])
{
echo $vysledek4[‘node’].”n”;
echo “—“.$vysledek4[‘level’].”n”;

}
$i++;
}
}[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@speghettiCodeApr 21.2014 — Instead of loops, what about:

[code=php]
<?php
echo 'here';
$arr=array(
array(

'node' => 'description',
'level' => 5
),
array(
'node' => 'book',
'level' => 4
),
array(

'node' => 'hi',
'level' => 5
),
array(
'node' => 'room',
'level' => 3
),
array(
'node' => 'library',
'level' => 2
)
);

echo "<pre>";print_r($arr);echo "</pre>";

function cmp($a, $b){
return $a['level'] - $b['level'];
}

usort($arr, "cmp");
echo "<pre>";print_r($arr);echo "</pre>";
?>
[/code]
Copy linkTweet thisAlerts:
@NogDogApr 21.2014 — You can even use an anonymous function (assuming a reasonably up-to-date version of PHP):
[code=php]
usort(
$arr,
function($a, $b) {
return $a['level'] - $b['level'];
}
);
[/code]

?
×

Success!

Help @hang_tran 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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