Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Zamiana tablicy z jednej struktury na inną, Jak najprościej?
Blackhole
post
Post #1





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

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


Cześć.
Załóżmy, że mam tablicę postaci:
  1. array(1=>array("name"=>"my_name1", "value"=>"val1"), 2=>("name"=>"my_name2", "value"=>"val2"), ...)
Jak najsprawniej zamienić taką tablicę w postać:
  1. array("name"=>array("my_name1", "my_name2", ...), "value"=>array("val1", "val2", ...))

Będę wdzięczny za pomoc...

P.s. Forum chyba ma jakieś problemy z edycją postów.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Damonsson
post
Post #2





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

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


array_merge_recursive
Go to the top of the page
+Quote Post
Blackhole
post
Post #3





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

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


Jakoś nie bardzo wiem, jak ją zastosować (IMG:style_emoticons/default/facepalmxd.gif)
  1. $form = array_merge_recursive($form);

nic nie daje. $form się nie zmienia.
Go to the top of the page
+Quote Post
Damonsson
post
Post #4





Grupa: Zarejestrowani
Postów: 2 355
Pomógł: 533
Dołączył: 15.01.2010
Skąd: Bydgoszcz

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


  1. $newform = array();
  2. foreach ($form as $value) {
  3. $newform = array_merge_recursive($newform, $value);
  4. }
  5. var_dump($newform);


Lepij?
Go to the top of the page
+Quote Post
Blackhole
post
Post #5





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

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


Popełniłem błąd na początku, bo tablica, którą chcę przetworzyć, jest postaci:
  1. array(6) {
  2. [0]=>
  3. array(2) {
  4. ["name"]=> string(6) "gru_id"
  5. ["value"]=> string(4) "id_1"
  6. }
  7. [1]=>
  8. array(2) {
  9. ["name"]=> string(8) "gru_name"
  10. ["value"]=> string(6) "name_1"
  11. }
  12. [2]=>
  13. array(2) {
  14. ["name"]=> string(6) "gru_id"
  15. ["value"]=> string(4) "id_2"
  16. }
  17. [3]=>
  18. array(2) {
  19. ["name"]=> string(8) "gru_name"
  20. ["value"]=> string(6) "name_2"
  21. }
  22. [4]=>
  23. array(2) {
  24. ["name"]=> string(6) "gru_id"
  25. ["value"]=> string(4) "id_3"
  26. }
  27. [5]=>
  28. array(2) {
  29. ["name"]=> string(8) "gru_name"
  30. ["value"]=> string(6) "name_3"
  31. }
  32. }
i chcę ją zamienić na:
  1. array(2) {
  2. ["gru_id"]=> array(3) {"id_1", "id_2", "id_3"}
  3. ["gru_name"]=> array(3) {"name_1", "name_2", "name_3"}
  4. }
Przepraszam za pomyłkę (IMG:style_emoticons/default/facepalmxd.gif)

Ten post edytował Blackhole 10.07.2014, 07:50:53
Go to the top of the page
+Quote Post
Pyton_000
post
Post #6





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


  1. $outpu = array();
  2. foreach($tablica as $values) {
  3. $output[$values['name']][] = $values['value'];
  4. }
Go to the top of the page
+Quote Post
Blackhole
post
Post #7





Grupa: Zarejestrowani
Postów: 283
Pomógł: 1
Dołączył: 15.11.2004
Skąd: Mikołów

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


Dzięki, Pyton_000 (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 1.10.2025 - 11:52