Zmiana kolejności kluczy tablicy PHP
Moja tablica wygląda tak:
array(
0 => 'val',
2 => 'val',
3 => 'val',
5 => 'val',
7 => 'val'
);
Jak mogę zmienić kolejność klawiszy tak, aby szły jak 0, 1, 2, 3, 4
?
48
Author: Emil Vikström, 2012-06-27
4 answers
Użyj array_values :
$reindexed_array = array_values($old_array);
109
Author: Emil Vikström,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-27 11:08:32
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-27 11:08:32
array_splice($old_array, 0, 0);
Nie posortuje tablicy i nie utworzy drugiej tablicy
12
Author: alekveritov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-14 01:10:05
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-14 01:10:05
Używając sort($array);
Zobacz dokumentację PHP tutaj .
Polecam sort
nad array_values
, ponieważ nie utworzy drugiej tablicy. Z poniższym kodem masz teraz dwie tablice zajmujące przestrzeń: $reindexed_array oraz $old_array. Niepotrzebnie.
$reindexed_array = array_values($old_array);
9
Author: Rawkode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-27 11:13:27
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-27 11:13:27
array_splice($jam_array, 0, count($jam_array));
Aby posortować tablicę z brakującymi indeksami pośrednimi, z count kolejność jest bardziej bezpieczna. Tak więc 0
jest pierwszym indeksem i count($jam_array)
lub sizeof($jam_array)
Zwraca pozycję dziesiętną tablicy, czyli ostatni indeks.
0
Author: fvlgnn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-21 12:32:21
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-21 12:32:21