Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Rozbicie tablicy na stringi
bliitz
post
Post #1





Grupa: Zarejestrowani
Postów: 155
Pomógł: 12
Dołączył: 5.04.2008

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


Witam

Załóżmy, że przykładowa tablica wygląda tak:

  1. $conditions = array( 'required', 'numeric' );


W jaki sposób można ją rozbić aby otrzymać dwa stringi, tak aby w każdym znajdowała się jedna wartość z tablicy?

Coś w ten deseń:

  1. $string1 = 'required';
  2. $string2 = 'numeric';


implode zwraca niestety jeden string, a mi są potrzebne oddzielne.

Ten post edytował bliitz 13.01.2010, 14:23:20
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
ddiceman
post
Post #2





Grupa: Zarejestrowani
Postów: 326
Pomógł: 121
Dołączył: 23.07.2008
Skąd: Wrocław

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


a moze lepiej:
  1. public function check( $post, array $conditions )
  2. {
  3. $this->post = $post;
  4. $this->conditions = new Validation( $this->post );
  5. $this->conditions->pre_filter( 'trim' );
  6. foreach( $conditions as $k => $v )
  7. {
  8. if(is_array($v)) $this->check( $post, $v ); //rekurencyjne wywolanie
  9. else $this->conditions->add_rules( $k, $v );
  10. }
  11. return $this->conditions->validate();
  12. }

?
Go to the top of the page
+Quote Post
bliitz
post
Post #3





Grupa: Zarejestrowani
Postów: 155
Pomógł: 12
Dołączył: 5.04.2008

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


Cytat(ddiceman @ 13.01.2010, 16:05:59 ) *
a moze lepiej:
  1. [url="./Pobierz-Plik-181690.html"]pobierz[/url], [url="Plaintext-181690.html"]plaintext[/url] [list=1][*] [url="./Pobierz-Plik-180389.html"]pobierz[/url], [url="Plaintext-180389.html"]plaintext[/url] [list=1][*]public function check( $post, [url="http://www.php.net/array"][url="http://www.php.net/array"]array[/url][/url] $conditions )[*] {[*] $this->post = $post;[*] $this->conditions = new Validation( $this->post );[*] $this->conditions->pre_filter( 'trim' );[*] foreach( $conditions as $k => $v )[*] {[*] if([url="http://www.php.net/is_array"][url="http://www.php.net/is_array"]is_array[/url][/url]($v)) $this->check( $post, $v ); //rekurencyjne wywolanie[*] else $this->conditions->add_rules( $k, $v );[*] }[*] return $this->conditions->validate();[*] }[/list][PHP] pobierz, plaintext[*]?<!--QuoteEnd--></div><!--QuoteEEnd-->[*][*]niestety ale gubi wtedy klucze i zwraca w takiej postaci[*][*][php][*]name required[*]surname required[*]0 required[*]1 numeric[*]birthday Array[*]0 required[*]1 numeric[*]height Array[*]0 required[*]1 numeric[*]weight Array[/list]


Niestety ale traci wtedy nazwy kluczy i zwraca

  1. name required
  2. surname required
  3. 0 required
  4. 1 numeric
  5. birthday Array
  6. 0 required
  7. 1 numeric
  8. height Array
  9. 0 required
  10. 1 numeric
  11. weight Array



Problem rozwiązałem w następujący sposób

  1. public function check( $post, array $conditions )
  2. {
  3. $this->post = $post;
  4. $this->validation = new Validation( $this->post );
  5. $this->validation->pre_filter( 'trim' );
  6.  
  7. foreach( $conditions as $k => $v )
  8. {
  9. if( is_array( $v ) )
  10. {
  11. $j = count( $v );
  12. for( $i = 0; $i < $j; ++$i )
  13. $this->validation->add_rules( $k, $v[ $i ] );
  14. }
  15. else
  16. $this->validation->add_rules( $k, $v );
  17. }
  18.  
  19. return $this->validation->validate();
  20. }


Ten post edytował bliitz 19.01.2010, 12:01:46
Go to the top of the page
+Quote Post

Posty w temacie


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: 7.10.2025 - 13:55