Answer the question
In order to leave comments, you need to log in
There is no "Add topic" button, where can I find it?
Free Wordpress account, no "Add Theme" button
Answer the question
In order to leave comments, you need to log in
In a PHP associative array, the order always follows the order in which the keys were added to it.
$array = [
[
"gq_address" => "188.120.254.140",
"gq_hostname" => "• RGPlay | DarkRP [Быстрая загрузка]",
"gq_maxplayers" => 128,
"gq_numplayers" => 0,
"gq_online" => true,
"gq_port_client" => 27015,
], [
"gq_address" => "62.109.18.242",
"gq_hostname" => "Default Breach Server",
"gq_maxplayers" => 128,
"gq_numplayers" => 0,
"gq_online" => true,
"gq_port_client" => 27015,
]
];
$array = array_map(function($el) {
return [
'gq_hostname' => $el['gq_hostname'],
'gq_address' => $el['gq_address'],
'gq_port_client' => $el['gq_port_client'],
'gq_online' => $el['gq_online'],
'gq_numplayers' => $el['gq_numplayers'],
'gq_maxplayers' => $el['gq_maxplayers']
];
}, $array);
var_dump($array);
array(2) {
[0] => array(6) {
["gq_hostname"] => string(55) "• RGPlay | DarkRP [Быстрая загрузка]"
["gq_address"] => string(15) "188.120.254.140"
["gq_port_client"] => int(27015)
["gq_online"] => bool(true)
["gq_numplayers"] => int(0)
["gq_maxplayers"] => int(128)
}
[1] => array(6) {
["gq_hostname"] => string(21) "Default Breach Server"
["gq_address"] => string(13) "62.109.18.242"
["gq_port_client"] => int(27015)
["gq_online"] => bool(true)
["gq_numplayers"] => int(0)
["gq_maxplayers"] => int(128)
}
}
uksort()
function
// Берем входящий массив:
$input = [
[
'gq_address' => '188.120.254.140',
'gq_hostname' => '• RGPlay | DarkRP [Быстрая загрузка]',
'gq_maxplayers' => 128,
'gq_numplayers' => 0,
'gq_online' => true,
'gq_port_client' => 27015,
],
[
'gq_address' => '62.109.18.242',
'gq_hostname' => 'Default Breach Server',
'gq_maxplayers' => 128,
'gq_numplayers' => 0,
'gq_online' => true,
'gq_port_client' => 27015,
],
];
// Определяем желаемый порядок ключей:
$order = [
'gq_hostname',
'gq_address',
'gq_port_client',
'gq_online',
'gq_numplayers',
'gq_maxplayers',
];
// Перебираем элементы входящего массива и сортируем их по ключам:
$output = array_map( function($array) use ($order)
{
// Эта функция сортирует по ключам
uksort( $array, function($a, $b) use ($order)
{
$a_desired_position = array_search($a, $order, true);
$b_desired_position = array_search($b, $order, true);
// Вот тут вся магия:
// нужно вернуть отрицательное число, 0 или положительное число,
// в зависимости от положения одного элемента относительно другого.
return $a_desired_position - $b_desired_position;
} );
return $array;
}, $input );
var_dump($input);
var_dump($output);
// Входящий массив:
array:2 [▼
0 => array:6 [▼
"gq_address" => "188.120.254.140"
"gq_hostname" => "• RGPlay | DarkRP [Быстрая загрузка]"
"gq_maxplayers" => 128
"gq_numplayers" => 0
"gq_online" => true
"gq_port_client" => 27015
]
1 => array:6 [▼
"gq_address" => "62.109.18.242"
"gq_hostname" => "Default Breach Server"
"gq_maxplayers" => 128
"gq_numplayers" => 0
"gq_online" => true
"gq_port_client" => 27015
]
]
// Отсортированный массив:
array:2 [▼
0 => array:6 [▼
"gq_hostname" => "• RGPlay | DarkRP [Быстрая загрузка]"
"gq_address" => "188.120.254.140"
"gq_port_client" => 27015
"gq_online" => true
"gq_numplayers" => 0
"gq_maxplayers" => 128
]
1 => array:6 [▼
"gq_hostname" => "Default Breach Server"
"gq_address" => "62.109.18.242"
"gq_port_client" => 27015
"gq_online" => true
"gq_numplayers" => 0
"gq_maxplayers" => 128
]
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question