Sort [key => value] array by another array of keys with same index [duplicate]
This question already has an answer here:
How can I sort arrays and data in PHP?
9 answers
I have an array of indexes that I want to sort my other array:
$order = [7, 2, 1, 4];
$array = [
1 => "O",
2 => "T"
4 => "F"
7 => "S"
]
How can I order the $array based on $order array, so that the output is..
$array = [
7 => "S",
2 => "T"
1 => "O",
4 => "F"
]
As far as I read, something other than for loop is much preferred
php arrays sorting
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 19:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I sort arrays and data in PHP?
9 answers
I have an array of indexes that I want to sort my other array:
$order = [7, 2, 1, 4];
$array = [
1 => "O",
2 => "T"
4 => "F"
7 => "S"
]
How can I order the $array based on $order array, so that the output is..
$array = [
7 => "S",
2 => "T"
1 => "O",
4 => "F"
]
As far as I read, something other than for loop is much preferred
php arrays sorting
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 19:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How can I sort arrays and data in PHP?
9 answers
I have an array of indexes that I want to sort my other array:
$order = [7, 2, 1, 4];
$array = [
1 => "O",
2 => "T"
4 => "F"
7 => "S"
]
How can I order the $array based on $order array, so that the output is..
$array = [
7 => "S",
2 => "T"
1 => "O",
4 => "F"
]
As far as I read, something other than for loop is much preferred
php arrays sorting
This question already has an answer here:
How can I sort arrays and data in PHP?
9 answers
I have an array of indexes that I want to sort my other array:
$order = [7, 2, 1, 4];
$array = [
1 => "O",
2 => "T"
4 => "F"
7 => "S"
]
How can I order the $array based on $order array, so that the output is..
$array = [
7 => "S",
2 => "T"
1 => "O",
4 => "F"
]
As far as I read, something other than for loop is much preferred
This question already has an answer here:
How can I sort arrays and data in PHP?
9 answers
php arrays sorting
php arrays sorting
asked Dec 28 '18 at 19:29
sentysenty
3,250551129
3,250551129
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 19:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 28 '18 at 19:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could make use of uksort and array_search:
uksort($array, function ($key1, $key2) use ($order) {
return array_search($key1, $order) - array_search($key2, $order);
});
Demo here: https://3v4l.org/TlWmP
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could make use of uksort and array_search:
uksort($array, function ($key1, $key2) use ($order) {
return array_search($key1, $order) - array_search($key2, $order);
});
Demo here: https://3v4l.org/TlWmP
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
add a comment |
You could make use of uksort and array_search:
uksort($array, function ($key1, $key2) use ($order) {
return array_search($key1, $order) - array_search($key2, $order);
});
Demo here: https://3v4l.org/TlWmP
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
add a comment |
You could make use of uksort and array_search:
uksort($array, function ($key1, $key2) use ($order) {
return array_search($key1, $order) - array_search($key2, $order);
});
Demo here: https://3v4l.org/TlWmP
You could make use of uksort and array_search:
uksort($array, function ($key1, $key2) use ($order) {
return array_search($key1, $order) - array_search($key2, $order);
});
Demo here: https://3v4l.org/TlWmP
answered Dec 28 '18 at 19:38
JetoJeto
5,05521018
5,05521018
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
add a comment |
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
Clear and simple! Thanks
– senty
Dec 28 '18 at 19:42
add a comment |