Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Przerobienie tablicy na inną
damianooo
post 10.05.2017, 15:41:45
Post #1





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


Potrzebuję przerobić taką tablicę:

  1. array(80) {
  2. [0]=>
  3. array(7) {
  4. ["meet_id"]=>
  5. int(1)
  6. ["host"]=>
  7. string(12) "FC Barcelona"
  8. ["guest"]=>
  9. string(11) "Real Madryt"
  10. ["hostType"]=>
  11. int(0)
  12. ["guestType"]=>
  13. int(2)
  14. ["username"]=>
  15. string(6) "Damian"
  16. ["term"]=>
  17. string(23) "Wtorek,12-12-2016,21:00"
  18. }
  19. [1]=>
  20. array(7) {
  21. ["meet_id"]=>
  22. int(2)
  23. ["host"]=>
  24. string(15) "Atletico Madryt"
  25. ["guest"]=>
  26. string(10) "Sevilla FC"
  27. ["hostType"]=>
  28. int(1)
  29. ["guestType"]=>
  30. int(2)
  31. ["username"]=>
  32. string(6) "Damian"
  33. ["term"]=>
  34. string(23) "Wtorek,12-12-2016,21:00"
  35. }
  36. ...


na taką:

  1. array(10) {
  2. [0] => array(3) {
  3. ["meet_id"] => 1,
  4. ["host"] => "team 1",
  5. ["guest"] => "team 2",
  6. ["types"] => array(10) {
  7. ["0-1","0-0","2-1","0-1","-","2-1","0-1","0-0","-","2-1"]
  8. }
  9. }
  10. [1] .... itd.
  11. }



Jak to zrobić ?
Go to the top of the page
+Quote Post
Pyton_000
post 10.05.2017, 16:48:56
Post #2





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

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


No i jaki masz problem? Prezlatujesz po tablicy, wybierasz interesujące dane i pchasz do innej tablicy.
Go to the top of the page
+Quote Post
damianooo
post 10.05.2017, 17:19:45
Post #3





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


Możesz podpowiedzieć co robię źle w poniższym ?

  1. foreach($result as $details){
  2. if(!in_array($details['meet_id'],$types)){
  3. $types[$details['meet_id']]['meet_id'][] = $details['meet_id'];
  4. $types[$details['meet_id']]['host'][] = $details['host'];
  5. $types[$details['meet_id']]['guest'][] = $details['guest'];
  6. $types[$details['meet_id']]['types'][] = $details['hostType'].' - '.$details['guestType'];
  7. }
  8. }


wynik jaki otrzymuję to :

  1. array(10) {
  2. [1]=>
  3. array(4) {
  4. ["meet_id"]=>
  5. string(8) "Array(8)"
  6. ["host"]=>
  7. string(8) "Array(8)"
  8. ["guest"]=>
  9. string(8) "Array(8)"
  10. ["types"]=>
  11. string(8) "Array(8)"
  12. }
  13. [2]=>
  14. array(4) {
  15. ["meet_id"]=>
  16. string(8) "Array(8)"
  17. ["host"]=>
  18. string(8) "Array(8)"
  19. ["guest"]=>
  20. string(8) "Array(8)"
  21. ["types"]=>
  22. string(8) "Array(8)"
  23. }
  24. ...


z góry dziękuję
Go to the top of the page
+Quote Post
Pyton_000
post 10.05.2017, 17:32:43
Post #4





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

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


Raczej coś w ten deseń:

  1. <?php
  2.  
  3. foreach ($result as $detail) {
  4. if(in_array($detail['meet_id'],$types)) {
  5. $types[$detail['meet_id']]['types'][] = $detail['hostType'].' - '.$details['guestType']
  6. continue;
  7. }
  8. $types[$detail['meet_id']] = [
  9. 'meet_id' => $detail['meet_id'],
  10. 'host' => $detail['host'],
  11. 'guest' => $detail['guest'],
  12. 'types' => [
  13. $detail['hostType'].' - '.$details['guestType']
  14. ],
  15. ];
  16. }
Go to the top of the page
+Quote Post
damianooo
post 10.05.2017, 19:50:23
Post #5





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


jest lepiej ...
ale jeszcze nie wiem czemu nie wypisuje mi typów tylko jest tak: ["types"]=>string(8) "Array(1)" ?

  1. array(10) {
  2. [1]=>
  3. array(4) {
  4. ["meet_id"]=>
  5. int(1)
  6. ["host"]=>
  7. string(12) "FC Barcelona"
  8. ["guest"]=>
  9. string(11) "Real Madryt"
  10. ["types"]=>
  11. string(8) "Array(1)"
  12. }
  13. [2]=>
  14. array(4) {
  15. ["meet_id"]=>
  16. int(2)
  17. ["host"]=>
  18. string(15) "Atletico Madryt"
  19. ["guest"]=>
  20. string(10) "Sevilla FC"
  21. ["types"]=>
  22. string(8) "Array(1)"
  23. }
  24. ...
Go to the top of the page
+Quote Post
Pyton_000
post 10.05.2017, 20:14:17
Post #6





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

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


literówka w `$details['guestType']` wywal `s`
Go to the top of the page
+Quote Post
damianooo
post 10.05.2017, 20:37:00
Post #7





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


to nie to bo już wcześniej to zauważyłem i poprawiłem i jest nadal tak samo..

Mam tak:

  1. foreach ($result as $detail) {
  2. if(in_array($detail['meet_id'],$types)) {
  3. $types[$detail['meet_id']]['types'][] = $detail['hostType'].' - '.$detail['guestType'];
  4. continue;
  5. }
  6. $types[$detail['meet_id']] = [
  7. 'meet_id' => $detail['meet_id'],
  8. 'host' => $detail['host'],
  9. 'guest' => $detail['guest'],
  10. 'types' => [
  11. $detail['hostType'].' - '.$detail['guestType']
  12. ],
  13. ];
  14. }


Ten post edytował damianooo 10.05.2017, 20:41:55
Go to the top of the page
+Quote Post
Pyton_000
post 10.05.2017, 21:27:05
Post #8





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

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


no to

var_dump($detail['hostType'], $detail['guestType']);
Go to the top of the page
+Quote Post
damianooo
post 10.05.2017, 21:30:40
Post #9





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


var_dump wyświetla bez problemu wartości tych pól :
  1. int(0)
  2. int(2)
  3. int(1)
  4. int(2)
  5. int(0)
  6. int(1)
  7. int(2)
  8. int(2)
  9. int(1)
  10. int(2)
  11. int(0)
  12. int(0)
  13. int(2)
  14. int(0)
Go to the top of the page
+Quote Post
Pyton_000
post 10.05.2017, 21:33:37
Post #10





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

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


a jak to potem wyświetlasz?
Go to the top of the page
+Quote Post
damianooo
post 10.05.2017, 21:36:25
Post #11





Grupa: Zarejestrowani
Postów: 496
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


ok rozkminiłem to ... ma być tak:

  1. foreach ($result as $detail) {
  2. if(!in_array($detail['meet_id'],$types)) {
  3. $types[$detail['meet_id']]['meet_id'] = $detail['meet_id'];
  4. $types[$detail['meet_id']]['host'] = $detail['host'];
  5. $types[$detail['meet_id']]['guest'] = $detail['guest'];
  6. $types[$detail['meet_id']]['types'][] = $detail['hostType'].' - '.$detail['guestType'];
  7. }
  8. }


dzięki Python za pomoc, pozdrawiam

Ten post edytował damianooo 11.05.2017, 08:04:48
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: 15.06.2025 - 15:59