Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] array_flip i unikanie kolizji
nemo27
post 10.05.2007, 14:45:24
Post #1





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 11.02.2003
Skąd: Mysłowice

Ostrzeżenie: (0%)
-----


Potrzebuję użyć funkcji array_flip do posortowania wyników tablicy lecz łącznie z nazwami kluczy.
Czyli:
a=3
b=2
c=1 etc.
Problem polega na tym, że jeśli znajdą się dwie nazwy o tej samej wartości np: 10 - to pozostaje tylko jej wartość bez nazwy.
Chodzi mi teraz jak rozwiązać tę kolizję tej funkcji, tzn jeśli będą dwie te same wartości to pojawiły się obie łącznie z nazwami.
  1. <?
  2. $sum_a = 12;
  3. $sum_b = 10;
  4. $sum_c = 12;
  5. $sum_d = 8;
  6. $sum_e = 9;
  7. $array = array ("suma" => $sum_a,"sumb" => $sum_b,"sumc" => $sum_c,"sumd" => $sum_d,"sume" => $sum_e);
  8.  
  9. ksort($array);
  10. $array = array_flip($array);
  11.  
  12. while (list($k,$v) = each ($array))
  13. { print ("<br>".$k."-".$v);
  14. }
  15.  
  16. ?>

Hmm

Ten post edytował nemo27 10.05.2007, 14:50:07
Go to the top of the page
+Quote Post
Darti
post 10.05.2007, 14:54:32
Post #2





Grupa: Zarejestrowani
Postów: 1 076
Pomógł: 62
Dołączył: 6.03.2005
Skąd: Wroc

Ostrzeżenie: (0%)
-----


W commentach do array_flip znalazłem:
  1. <?php
  2. function array_invert($arr)
  3. {
  4. $res = Array();
  5. foreach(array_keys($arr) as $key)
  6. {
  7. if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
  8. array_push($res[$arr[$key]], $key);
  9. }
  10. return $res;
  11. }
  12. ?>


--------------------
The answer is out there, Neo. It's looking for you. And it will find you, if you want it to.
SERVER_SOFTWARE : Apache/2.2.4 (Win32) PHP/5.2.1
MySQL Client API version : 5.0.27
Go to the top of the page
+Quote Post
nemo27
post 15.05.2007, 09:35:02
Post #3





Grupa: Zarejestrowani
Postów: 17
Pomógł: 0
Dołączył: 11.02.2003
Skąd: Mysłowice

Ostrzeżenie: (0%)
-----


Ok, ale jak robię tak:
  1. <?
  2. $sum_a = 12;
  3. $sum_b = 10;
  4. $sum_c = 12;
  5. $sum_d = 8;
  6. $sum_e = 9;
  7. $array = array ("suma" => $sum_a,"sumb" => $sum_b,"sumc" => $sum_c,"sumd" => $sum_d,"sume" => $sum_e);
  8.  
  9. function array_invert($arr)
  10. {
  11. $res = Array();
  12. foreach(array_keys($arr) as $key)
  13. {
  14. if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
  15. array_push($res[$arr[$key]], $key);
  16. }
  17. return $res;
  18. }
  19.  
  20.  
  21. array_invert($array);
  22.  
  23. while (list($k,$v) = each ($array))
  24. { print ("<br>".$k."-".$v);
  25. }
  26.  
  27.  
  28. ?>


To znów otrzymuję kolejność:
suma-12
sumb-10
sumc-12
sumd-8
sume-9
a powinno być:
suma
sumc
sumb
sume
sumd
W czym błąd?

Ok EUREKA winksmiley.jpg znalazłem - może się to komuś przyda:
  1. <?php
  2. $sum_a = 12;
  3. $sum_b = 10;
  4. $sum_c = 12;
  5. $sum_d = 8;
  6. $sum_e = 9;
  7. $array = array ("suma" => $sum_a,"sumb" => $sum_b,"sumc" => $sum_c,"sumd" => $sum_d,"sume" => $sum_e);
  8.  
  9.  
  10. function array_invert($arr)
  11. {
  12. $res = Array();
  13. foreach(array_keys($arr) as $key)
  14. {
  15. if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
  16. array_push($res[$arr[$key]], $key);
  17. }
  18. return $res;
  19. }
  20. array_invert($array);
  21. arsort($array);
  22.  
  23. while (list($k,$v) = each ($array))
  24. { print ("<br>".$k."-".$v);
  25. }
  26. ?>

Pozdro smile.gif
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 12.06.2025 - 23:24