Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [PHP]Tablicowa zagwozdka
nansss93
post
Post #1





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Witam wszystkich,

Mam pewien problem.

napisałem sobie funkcję:

  1. array_filter($result, array($this,'removeDuplicated'));
  2.  
  3. protected function removeDuplicated(&$a){
  4. $count = count($a);
  5. for($j=$count-1; $j>0; $j--){
  6. for($i=0; $i<$j; $i++){
  7. if(isset($a[$i]) && isset($a[$i+1])){
  8. if($a[$i][1]==$a[$i+1][1]){
  9. unset($a);
  10. }
  11. }
  12. }
  13. }
  14. }


Niestety tablica dla, której dwie wartości są takie same nie jest usuwana.
Nie bardzo wiem co jest nie tak.

Struktura $result:
Kod
Array(
    [key] => Array
        [0] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [1] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [2] => Array
                (
                   [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [3] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [4] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [5] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [6] => Array
                (
                   [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [7] => Array
                (
                    [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )

            [8] => Array
                (
                   [0] =>
                    [1] =>
                    [2] =>
                    [3] =>
                    [4] =>
                    [5] =>
                )
    )
)


Go to the top of the page
+Quote Post
Pyton_000
post
Post #2





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

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


Raz że tablica jest pusta...
pokaż tablicę wejściową, tablicę wyjściową, i opisz warunek który musi zajść żeby $a => $a'
Go to the top of the page
+Quote Post
nansss93
post
Post #3





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Tablica nie jest pusta.

Tablica wejściowa i wyjściowa jest identyczna i zawiera 919 elementów.

Tablica [key] ma zostać usunięta, jeśli np [key][1][1]==[key][2][1]. Generalnie jeśli wartość w [1] chociaż dla dwóch jest taka sama
Go to the top of the page
+Quote Post
trueblue
post
Post #4





Grupa: Zarejestrowani
Postów: 6 807
Pomógł: 1828
Dołączył: 11.03.2014

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


http://stackoverflow.com/a/6654634/5889778
Go to the top of the page
+Quote Post
nansss93
post
Post #5





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Niestety nic to nie zmienia.
Próbowałem wcześniej tego rozwiązania.

Ten post edytował nansss93 16.05.2016, 13:52:39
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




array_filter sluzy do filtrowania - patrz manual.

  1. $result = array_filter($result, array($this,'removeDuplicated'));
  2.  
  3. protected function removeDuplicated($a){
  4. $count = count($a);
  5. for($j=$count-1; $j>0; $j--){
  6. for($i=0; $i<$j; $i++){
  7. if(isset($a[$i]) && isset($a[$i+1])){
  8. if($a[$i][1]==$a[$i+1][1]){
  9. return false;
  10. }
  11. }
  12. }
  13. }
  14. return true;
  15. }

przy zalozeniu rzecz jasna, ze twoje FOR i warunki sa poprawne - tego nie sprawdzalem
Go to the top of the page
+Quote Post
nansss93
post
Post #7





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Fakt faktem po Twoim rozwiązaniu
  1. count($result);
przed wykonaniem array_filter oraz po jest różny, ale przykład, który wiem, że się powtarzają wartości, w dalszym ciągu istnieje:

  1. print_r($result['RON7-US-XER-5']);
  2. $result = array_filter($result, array($this,'removeDuplicated'));
  3.  
  4. print_r($result['RON7-US-XER-5']);exit;


print_r przed i po zwraca:

Kod
Array
(
    [1] => Array
        (
              [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [3] => Array
        (
              [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [4] => Array
        (
              [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [5] => Array
        (
             [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [6] => Array
        (
              [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [7] => Array
        (
             [0] =>
            [1] =
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [8] => Array
        (
            [0] =>
            [1] => data-LV3
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [9] => Array
        (
            [0] =>
            [1] => data-LV3
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

)


a moje założenie jest takie, że przy wywołaniu print_r po array_filter powinienem dostać undefined lub chociaż pustą tablicę. Jednak wolałbym, żeby została ona usunięta.
Go to the top of the page
+Quote Post
nospor
post
Post #8





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Mi tam dla danych testowych dziala. Miales jeden blad z j > 0

  1. $result = [
  2. 'blabla' => [
  3. ['aa','bb','ccc'],
  4. ['aa','bb','ccc'],
  5. ['aa','bbr','ccc'],
  6. ]
  7. ];
  8.  
  9. //print_r($result);
  10. $result = array_filter($result, 'removeDuplicated');
  11.  
  12. function removeDuplicated($a){
  13. //print_r($a);
  14. $count = count($a);
  15. for($j=$count-1; $j>=0; $j--){
  16. for($i=0; $i<$j; $i++){
  17. if(isset($a[$i]) && isset($a[$i+1])){
  18. if($a[$i][1]==$a[$i+1][1]){
  19. return false;
  20. }
  21. }
  22. }
  23. }
  24. return true;
  25. }
  26.  

Jak mowilem sprawdz swoje warunki, ja mam inne rzeczy do zrobienia (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
nansss93
post
Post #9





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Faktycznie. Dla testowych danych działa.
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Twoj kod w tej chwili sprawdza tylko sasiadujace elementy.

edit:

Tak na szybko
  1. function removeDuplicated($a){
  2. $tab = [];
  3. foreach ($a as $aa) {
  4. if (!isset($tab[$aa[1]])) {
  5. $tab[$aa[1]] = true;
  6. } else {
  7. return false;
  8. }
  9. }
  10. return true;
  11. }
Go to the top of the page
+Quote Post
nansss93
post
Post #11





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Dzięki za pomoc (IMG:style_emoticons/default/smile.gif)

O to chodziło

EDIT::
--------------------------------------------------------------------------------------------------
Temat niestety powrócił, ale z troszkę inną funkcją.

Muszę wyrzucić z tablicy tablice, w której brakuje danych pomiędzy.

Przykładowe dane:

Kod
Array
(
    [1] => Array
        (
            [0] =>
            [1] => data-LV0
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [3] => Array
        (
            [0] =>
            [1] => data-LV1.5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [4] => Array
        (
            [0] =>
            [1] => data-LV2
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [5] => Array
        (
            [0] =>
            [1] => data-LV4
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [6] => Array
        (
            [0] =>
            [1] => data-LV5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [7] => Array
        (
            [0] =>
            [1] => data-LV6
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [8] => Array
        (
            [0] =>
            [1] => data-LV7
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [9] => Array
        (
            [0] =>
            [1] => data-LV8
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

)



Jak widać brakuje data-LV3, więc taka tablica powinna zostać usunięta.

Starałem się napisać coś na podobieństwo kodu nospor, ale coś źle filtruje mi te dane:

KOD:

  1. protected function removeArrayIfThereIsMissingLevel($a){
  2. $count = count($a);
  3. $tab = [];
  4. for($i=0; $i<$count-1; $i++){
  5. if(isset($a[$i]) && isset($a[$i+1])){
  6. $from1 = stripos($a[$i][1],'lv');
  7. $from2 = stripos($a[$i+1][1],'lv');
  8. $to1 = stripos($a[$i+1][1],'.');
  9. $to2 = stripos($a[$i+1][1],'.');
  10. if($from1!=false && $from2!=false){
  11. $stringToCheck1 = substr($a[$i][1],$from1+2,$to1+1);
  12. $stringToCheck2 = substr($a[$i+1][1],$from2+2,$to2+1);
  13. if($stringToCheck1+1 == $stringToCheck2){
  14. $tab[$a[$i][2]] = true;
  15. } else {
  16. return false;
  17. }
  18. }
  19. }
  20. }
  21. return true;
  22. }


Potrzebuje jakiejś podpowiedzi

Ten post edytował nansss93 24.05.2016, 12:03:02
Go to the top of the page
+Quote Post
Pyton_000
post
Post #12





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

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


Ale skąd ma być usunięta... Przecież w tablicy źródłowej nie ma tej wartości więc nie masz skąd jej usunąć.
Go to the top of the page
+Quote Post
nospor
post
Post #13





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




@Pyton i po raz kolejny: czytaj tresc zadan od poczatku (IMG:style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #14





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

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


  1. <?php
  2. protected function removeArrayIfThereIsMissingLevel($array) {
  3. $cols = array_column($array, 2);
  4. array_walk($cols, function(&$val) {
  5. $val = floor(preg_replace('/data-LV([0-9]+)/', '$1', $val));
  6. });
  7.  
  8. if(array_diff(range(min($cols), max($cols)), $cols)) {
  9. return false;
  10. }
  11. return true;
  12. }


Ten post edytował Pyton_000 24.05.2016, 12:48:41
Go to the top of the page
+Quote Post
nansss93
post
Post #15





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Niestety powyższa metoda nie filtruje danych. Po wykonaniu metody ilość danych jest taka sama.


Dokładniejsza struktura:

Kod
Array
(
    [key1] => Array
    (
    [1] => Array
        (
            [0] =>
            [1] => data-LV0
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [3] => Array
        (
            [0] =>
            [1] => data-LV1.5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [4] => Array
        (
            [0] =>
            [1] => data-LV2
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [5] => Array
        (
            [0] =>
            [1] => data-LV4
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [6] => Array
        (
            [0] =>
            [1] => data-LV5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [7] => Array
        (
            [0] =>
            [1] => data-LV6
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [8] => Array
        (
            [0] =>
            [1] => data-LV7
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [9] => Array
        (
            [0] =>
            [1] => data-LV8
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    )

    [key2] => Array
    (
    [1] => Array
        (
            [0] =>
            [1] => data-LV0
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [3] => Array
        (
            [0] =>
            [1] => data-LV1.5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [4] => Array
        (
            [0] =>
            [1] => data-LV2
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [5] => Array
        (
            [0] =>
            [1] => data-LV4
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [6] => Array
        (
            [0] =>
            [1] => data-LV5
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [7] => Array
        (
            [0] =>
            [1] => data-LV6
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [8] => Array
        (
            [0] =>
            [1] => data-LV7
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    [9] => Array
        (
            [0] =>
            [1] => data-LV8
            [2] =>
            [3] =>
            [4] =>
            [5] =>
        )

    )

)



Ten post edytował nansss93 24.05.2016, 13:09:27
Go to the top of the page
+Quote Post
Pyton_000
post
Post #16





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

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


zmień w "$cols = array_column($array, 2);" na 1
Go to the top of the page
+Quote Post
nansss93
post
Post #17





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


Bez zmian. Zwrotka jaką dostaję:

Kod
[b] => Array
        (
            [3] => Array
                (
                    [0] => a
                    [1] => b-Lv0.9
                    [2] => b
                    [3] => c
                    [4] => d
                    [5] => e
                )

            [4] => Array
                (
                    [0] => a
                    [1] => b-Lv3.5
                    [2] => b
                    [3] => c
                    [4] => d
                    [5] => e
                )

        )


A nie spełnia to założenia. Brakuje LV1 oraz LV2 pomiędzy

Ten post edytował nansss93 24.05.2016, 13:44:47
Go to the top of the page
+Quote Post
Pyton_000
post
Post #18





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

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


wcześniej dawałeś "data-LV"... teraz masz "b-Lv"
Go to the top of the page
+Quote Post
nansss93
post
Post #19





Grupa: Zarejestrowani
Postów: 45
Pomógł: 1
Dołączył: 27.10.2015

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


To były przykładowe dane
Go to the top of the page
+Quote Post
com
post
Post #20





Grupa: Zarejestrowani
Postów: 3 034
Pomógł: 366
Dołączył: 24.05.2012

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


Pyton_000 zrób mu to uniwersalne bo to chyba lecą przykładowe dane (IMG:style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #21





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

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


@com i co jeszcze ? Nie dość że gotowiec to jeszcze mam się zastanawiać? A może tam po prostu jest kupa, albo każdy klucz inny....
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
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: 17.10.2025 - 21:05