Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> [PHP] Długości stringa w tablicy
Kshyhoo
post 22.03.2016, 11:03:12
Post #1





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Jak wyszukać w tablicy wielowymiarowej najdłuższe stringi? I w indeksach i wartościach. Czyli mam np. taką tablicę:
  1. (
  2. [0] => Array
  3. (
  4. [Nr] => 1
  5. [Nazwa] => jakiś tam string
  6. [Czas] => 2016-10-18 08:38:18
  7. [Opis] => -
  8. )
  9. [1] => Array
  10. (
  11. [Nr] => 2
  12. [Nazwa] => jeszcze inny string
  13. [Czas] => 2016-10-18 08:39:12
  14. [Opis] => -
  15. )
  16. [2] => Array
  17. (
  18. [Nr] => 3
  19. [Nazwa] => a tu jeszcze jeden string
  20. [Czas] => 2016-10-18 08:43:09
  21. [Opis] => -
  22. )
  23. )

Czyli powinienem otrzymać:
2016-10-18 08:38:18
2016-10-18 08:39:12
a tu jeszcze jeden string


I teraz chcę wyszukać w tablicy największe wartości dla każdego indeksu. Z bazy danych nie ma problemu:
  1. $all = sqlite_fetch_all($wynik, SQLITE_ASSOC);
  2. $kolumn = sqlite_num_fields($wynik);
  3. for($i=0;$i<$kolumn;$i++) {
  4. $dlugosci[$i] = strlen(sqlite_field_name($wynik, $i));
  5. for($j=0;$j<count($all);$j++) {
  6. $dlugosci[$i] = max($dlugosci[$i], strlen($all[$j][sqlite_field_name($wynik, $i)]));
  7. }
  8. }

A z tablicy nie mogę zajarzyć...

Nie pomoże nikt?


--------------------
Go to the top of the page
+Quote Post
phpion
post 22.03.2016, 11:09:25
Post #2





Grupa: Moderatorzy
Postów: 6 070
Pomógł: 860
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Spróbuj tego:
  1. $array = array(); // Twoja tablica
  2. $return = array(); // Tablica zwrotna
  3.  
  4. foreach ($array as $items) {
  5. $strlen_max = NULL; // NULL zeby nawet pusty string byl dluzszy
  6. $string = NULL;
  7.  
  8. foreach ($items as $item) {
  9. $strlen = mb_strlen($item);
  10.  
  11. if ($strlen_max === NULL || $strlen > $strlen_max) {
  12. $string = $item;
  13. $strlen_max = $strlen;
  14. }
  15. }
  16.  
  17. if ($string !== NULL) {
  18. $return[] = $string;
  19. }
  20. }
  21.  
  22. print_r($return);
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 11:25:49
Post #3





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Zwraca mi pustą tablicę.
  1. foreach ($aTempData as $jedenWymiar) {
  2. $najdluzszyString = "";
  3. $najdluzszyKlucz = "";
  4. foreach ($jedenWymiar as $klucz => $jedenElement) {
  5. if (strlen(utf8_decode($jedenElement)) > strlen(utf8_decode($najdluzszyString)))
  6. $najdluzszyString = $jedenElement;
  7.  
  8. if(strlen(utf8_decode($klucz)) > strlen(utf8_decode($najdluzszyKlucz)))
  9. $najdluzszyKlucz = $klucz;
  10. }
  11.  
  12.  
  13. echo $najdluzszyKlucz . " - ";
  14. echo $najdluzszyString . "<br/>";
  15. }

To powyższe zwraca mi tablicę z najdłuższymi stringami, zarówno kluczami jaki wartościami. Ja chciałbym jednak obliczyć uzyskać ich długości. No i nie wiem, jak to po za tym kodem użyć... Co wykombinuję, to mi nie wychodzi to, co bym chciał.

OK. Tak osiągnę długości:
  1. $najdluzszyKlucz = strlen(utf8_decode($najdluzszyKlucz)) . " - ";
  2. $najdluzszyString = strlen(utf8_decode($najdluzszyString)) . "<br/>";

Ale jak $najdluzszyString po za tym kodem?

I podobno można prościej za pomocą array_column, ale za krótki jestem...


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 11:34:22
Post #4





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Możliwe, że nie do końca zrozumiałem problem, ale może coś takiego:

  1. $arr = [
  2. '1'=> [
  3. '1'=>"asdasdasd",
  4. '22'=>'asdasasdasdasd',
  5. ],
  6. '2'=> [
  7. '333333'=>"asdasdasd",
  8. '4444'=>'asdasasdasdasd',
  9. ]
  10. ];
  11.  
  12. function getLongestKeyAndValue($array, $currentKey = "", $currentValue = ""){
  13. foreach($array as $k=>$v){
  14. if(strlen($currentKey) < strlen($k)){
  15. $currentKey = $k;
  16. }
  17. if(is_array($v)){
  18. $return = getLongestKeyAndValue($array, $currentKey, $currentValue);
  19. $currentKey = $return['key'];
  20. $currentValue = $return['value'];
  21. }else{
  22. if(strlen($currentValue) < strlen($v)){
  23. $currentValue = $v;
  24. }
  25. }
  26. }
  27. return ['key'=>$currentKey, 'value'=>$currentValue];
  28. }
  29.  
  30. foreach($arr as $value){
  31. $ret = getLongestKeyAndValue($value);
  32. var_dump($ret);
  33. }
  34.  


questionmark.gif
Go to the top of the page
+Quote Post
phpion
post 22.03.2016, 11:42:49
Post #5





Grupa: Moderatorzy
Postów: 6 070
Pomógł: 860
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Przecież podany przeze mnie kod działa:
  1. $array = array(
  2. 'Nr' => '1',
  3. 'Nazwa' => 'jakiś tam string',
  4. 'Czas' => '2016-10-18 08:38:18',
  5. 'Opis' => '-'
  6. ),
  7. 'Nr' => '2',
  8. 'Nazwa' => 'jeszcze inny string',
  9. 'Czas' => '2016-10-18 08:39:12',
  10. 'Opis' => '-'
  11. ),
  12. 'Nr' => '3',
  13. 'Nazwa' => 'a tu jeszcze jeden string',
  14. 'Czas' => '2016-10-18 08:43:09',
  15. 'Opis' => '-'
  16. )
  17. ); // Twoja tablica
  18. $return = array(); // Tablica zwrotna
  19.  
  20. foreach ($array as $items) {
  21. $strlen_max = NULL; // NULL zeby nawet pusty string byl dluzszy
  22. $string = NULL;
  23.  
  24. foreach ($items as $item) {
  25. $strlen = mb_strlen($item);
  26.  
  27. if ($strlen_max === NULL || $strlen > $strlen_max) {
  28. $string = $item;
  29. $strlen_max = $strlen;
  30. }
  31. }
  32.  
  33. if ($string !== NULL) {
  34. $return[] = $string;
  35. }
  36. }
  37.  
  38. print_r($return);

W efekcie otrzymujesz:
Kod
Array
(
    [0] => 2016-10-18 08:38:18
    [1] => jeszcze inny string
    [2] => a tu jeszcze jeden string
)

Różnica w stosunku do tego co podałeś:
Cytat
2016-10-18 08:38:18
2016-10-18 08:39:12
a tu jeszcze jeden string

jest taka, że drugi element to u mnie Nazwa, a nie Czas. Wynika to z tego, że zwraca pierwszy najdłuższy ciąg znaków. Jeśli chcesz zwrócić ostatni to wystarczy zmienić:
  1. if ($strlen_max === NULL || $strlen > $strlen_max) {

na:
  1. if ($strlen_max === NULL || $strlen >= $strlen_max) {
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 11:45:59
Post #6





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Prawie smile.gif Otrzymuję:
  1. array(2) {
  2. ["key"]=>
  3. int(22)
  4. ["value"]=>
  5. string(14) "asdasasdasdasd"
  6. }
  7.  
  8. array(2) {
  9. ["key"]=>
  10. int(333333)
  11. ["value"]=>
  12. string(14) "asdasasdasdasd"
  13. }

A chciałbym wartości liczbowe stringów, najlepiej jako jedną tablicę. Czyli
  1. [0] => 23
  2. [1] => 15
  3. [2] => 0
  4. [3] => 5
  5. [4] => 44
  6. )

Gdzie [0] to długość klucza (bo wszędzie taki sam) a reszta to wartości...

@phpion, zgadza się. Użyłem innych danych.


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 11:53:54
Post #7





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


  1. function getLongestKeyAndValue($array, $currentKey = 0, $currentValue = 0){
  2. foreach($array as $k=>$v){
  3. if($currentKey < strlen($k)){
  4. $currentKey = strlen($k);
  5. }
  6. if(is_array($v)){
  7. $return = getLongestKeyAndValue($array, $currentKey, $currentValue);
  8. $currentKey = $return['key'];
  9. $currentValue = $return['value'];
  10. }else{
  11. if($currentValue < strlen($v)){
  12. $currentValue = strlen($v);
  13. }
  14. }
  15. }
  16. return ['key'=>$currentKey, 'value'=>$currentValue];
  17. }
  18.  
  19. $result = [];
  20. foreach($arr as $value){
  21. $result[0] = $value['key'];
  22. $result[] = $value['value'];
  23. }
  24. var_dump($result);

Jest ok ?
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 12:24:52
Post #8





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Warning: Invalid argument supplied for foreach()
  1. foreach($arr as $value){


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 12:27:41
Post #9





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Mam wrażenie, że mnie trollujesz....
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 12:39:13
Post #10





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Gdzież bym śmiał...

To działa na Twojej tablicy, ale i tak otrzymuję pustą:
  1. (
  2. [0] =>
  3. [1] =>
  4. [2] =>
  5. )


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 12:46:33
Post #11





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Bah, klepałem kod "na oko", bez testów i był bład, spróbuj podmienić końcówkę na to:
  1. $result = [];
  2. foreach($arr as $value){
  3. $temp = getLongestKeyAndValue($value);
  4. $result[0] = $temp['key'];
  5. $result[] = $temp['value'];
  6. }

Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 13:13:31
Post #12





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




I to jest chyba to smile.gif Jeszcze tylko pokombinuje na okazję pustych stringów smile.gif

Już wiem, jaki jest powód nie działania na mojej tablicy. Moja ma index od 0:
  1. (
  2. [0] => Array
  3. (
  4. [Nr] => 1
  5. [Nazwa] => Adam Kowalski
  6. [Czas] => 2016-10-18 08:38:18
  7. [Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx] => -
  8. )
  9.  
  10. [1] => Array
  11. (
  12. [Nr] => 2
  13. [Nazwa] => Barbara Kowalska
  14. [Czas] => 2016-10-18 08:39:12
  15. [Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx] => -
  16. )
  17. )

A Twoja od 1.
  1. (
  2. [1] => Array
  3. (
  4. [1] => asdasdasd
  5. [22] => asdasasdasdasd
  6. )
  7.  
  8. [2] => Array
  9. (
  10. [333333] => asdasdasd
  11. [4444] => asdasasdasdasd
  12. )
  13.  
  14. )

Dlatego sypie wyniki.


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 13:23:42
Post #13





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Nie czaje, jak to sypie wyniki?

  1. <?php
  2. $array = array
  3. (
  4. 0 => array
  5. (
  6. 'Nr' => '1',
  7. 'Nazwa' => 'Adam Kowalski',
  8. 'Czas' => '2016-10-18 08:38:18',
  9. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => '-',
  10. ),
  11.  
  12. 1 => array
  13. (
  14. 'Nr' => '2',
  15. 'Nazwa' => 'Barbara Kowalska',
  16. 'Czas' => '2016-10-18 08:39:12',
  17. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => '-'
  18. )
  19. );
  20.  
  21. function getLongestKeyAndValue($array, $currentKey = 0, $currentValue = 0)
  22. {
  23. foreach ($array as $k => $v) {
  24. if ($currentKey < strlen($k)) {
  25. $currentKey = strlen($k);
  26. }
  27. if (is_array($v)) {
  28. $return = getLongestKeyAndValue($array, $currentKey, $currentValue);
  29. $currentKey = $return['key'];
  30. $currentValue = $return['value'];
  31. } else {
  32. if ($currentValue < strlen($v)) {
  33. $currentValue = strlen($v);
  34. }
  35. }
  36. }
  37. return ['key' => $currentKey, 'value' => $currentValue];
  38. }
  39.  
  40. $result = [];
  41.  
  42.  
  43. foreach ($array as $value) {
  44. $temp = getLongestKeyAndValue($value);
  45. $result[0] = $temp['key'];
  46. $result[] = $temp['value'];
  47. }
  48. var_dump($array);
  49. var_dump($result);


zwraca mi:
  1. array (size=2)
  2. 0 =>
  3. array (size=4)
  4. 'Nr' => string '1' (length=1)
  5. 'Nazwa' => string 'Adam Kowalski' (length=13)
  6. 'Czas' => string '2016-10-18 08:38:18' (length=19)
  7. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => string '-' (length=1)
  8. 1 =>
  9. array (size=4)
  10. 'Nr' => string '2' (length=1)
  11. 'Nazwa' => string 'Barbara Kowalska' (length=16)
  12. 'Czas' => string '2016-10-18 08:39:12' (length=19)
  13. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => string '-' (length=1)
  14.  
  15. array (size=3)
  16. 0 => int 50
  17. 1 => int 19
  18. 2 => int 19

Czyli tak jakby działa...

Ten post edytował kapslokk 22.03.2016, 13:24:17
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 13:40:00
Post #14





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Działa, działa. coś pokićkałęm z tablicą wcześniej.
Jak Twój kod ma się do UTF-8?


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 13:43:06
Post #15





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Wg tego raczej kiepsko:
http://wpengineer.com/2410/dont-use-strlen/
Ale zawsze możesz zamienić strlen na mb_strlen .
Go to the top of the page
+Quote Post
Kshyhoo
post 22.03.2016, 14:49:48
Post #16





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




No właśnie, z tym kodem co kombinowałem liczyło dobrze, ale nie umiałem uzyskać poza pętlą danych:
  1. foreach ($aTempData as $jedenWymiar) {
  2. $najdluzszyString = "";
  3. $najdluzszyKlucz = "";
  4. foreach ($jedenWymiar as $klucz => $jedenElement) {
  5. if (strlen(utf8_decode($jedenElement)) > strlen(utf8_decode($najdluzszyString)))
  6. $najdluzszyString = $jedenElement;
  7.  
  8. if(strlen(utf8_decode($klucz)) > strlen(utf8_decode($najdluzszyKlucz)))
  9. $najdluzszyKlucz = $klucz;
  10. }
  11.  
  12. $najdluzszyKlucz = strlen(utf8_decode($najdluzszyKlucz));
  13. $najdluzszyString = strlen(utf8_decode($najdluzszyString));
  14.  
  15. echo $najdluzszyKlucz . " - ";
  16. echo $najdluzszyString . "<br/>";
  17. }


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 22.03.2016, 15:01:22
Post #17





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


No dobra, ale masz jeszcze z czymś problem czy już nie?
Go to the top of the page
+Quote Post
Kshyhoo
post 23.03.2016, 21:40:44
Post #18





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Już, nie. Teraz sprawdzam, jak zachowuje się z polskimi znakami.
Wielkie dzięki za pomoc.

Piszę ponownie, bo zmieniłem koncepcję. Chciałbym uzyskać jedną tablicę, niezależnie od wartość indeksów i wartości tablicy. Czyli chcę znaleźć najdłuższe stringi w tablicy, niezależnie od tego, czy są w indeksach czy w wartościach.
Z tablicy w pierwszym poście będzie tak:
Cytat
2 - bo: Nr - z indeksu
25 - bo: a tu jeszcze jeden string - z wartości
19 - bo: 2016-10-18 08:38:18 - z wartości
4 - bo: Opis - z indeksu


--------------------
Go to the top of the page
+Quote Post
kapslokk
post 24.03.2016, 08:10:09
Post #19





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


  1. <?php
  2. $array = array
  3. (
  4. 0 => array
  5. (
  6. 'Nr' => '1',
  7. 'Nazwa' => 'Adam Kowalski',
  8. 'Czas' => '2016-10-18 08:38:18',
  9. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx-',
  10. ),
  11.  
  12. 1 => array
  13. (
  14. 'Nr' => '2',
  15. 'Nazwa' => 'Barbara Kowalska',
  16. 'Czas' => '2016-10-18 08:39:12',
  17. 'Opis ble ble ble ble ble ble ble xxxxxxxxxxxxxxxxx' => '-'
  18. )
  19. );
  20.  
  21. function getLongestKeyOrValue($array, $currentValue = 0)
  22. {
  23. foreach ($array as $k => $v) {
  24. if ($currentValue < strlen($k)) {
  25. $currentValue = strlen($k);
  26. }
  27. if (is_array($v)) {
  28. $currentValue = getLongestKeyOrValue($array, $currentValue);
  29. } else {
  30. if ($currentValue < strlen($v)) {
  31. $currentValue = strlen($v);
  32. }
  33. }
  34. }
  35. return $currentValue;
  36. }
  37.  
  38. $result = [];
  39.  
  40.  
  41. foreach ($array as $value) {
  42. $longestValue = getLongestKeyOrValue($value);
  43. $result[] = $longestValue;
  44. }
  45. var_dump($array);
  46. var_dump($result);


Ale to tak banalna zmiana, że serio mogłeś ją zrobić sam biggrin.gif
Go to the top of the page
+Quote Post
Kshyhoo
post 24.03.2016, 09:22:00
Post #20





Grupa: Opiekunowie
Postów: 3 855
Pomógł: 317
Dołączył: 4.01.2005
Skąd: że




Niestety, za cienki jestem. Ponadto, nie o to mi chodziło wink.gif
Zauważ, że tablica ma tylko 4 indeksy i tym samym 4 wartości:
Cytat
'Nr' => '1',
'Nazwa' => 'Adam Kowalski',
'Czas' => '2016-10-18 08:38:18',
'Opis' => 'woda'

Chciałbym wyciągnąć najdłuższe długości stringów, niezależnie czy to będzie indeks czy wartość. Czyli w tym przypadku, powinienem otrzymać tablicę tylko z 4 wartościami:
  1. (
  2. [0] => 2 bo 'Nr'
  3. [1] => 13 bo 'Adam Kowalski'
  4. [2] => 19 bo '2016-10-18 08:38:18'
  5. [3] => 4 bo 'Opis' albo 'woda', są identyczne długości
  6. )

Nie wiem, jak mam zebrać klucze i wartości do jednej kupy.


--------------------
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
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: 23.04.2024 - 18:32