Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Rozbicie tablicy na stringi
Forum PHP.pl > Forum > Przedszkole
bliitz
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.
ddiceman
co prawda wystarczyloby Ci uzycie
  1. $conditions[0] = 'required';
  2. $conditions[1] = 'numeric';

no ale jak juz cos, to (za skarby nie moge wymyslec, po co smile.gif ) rozwiazaniem dla Ciebie sa tzw. "zmienne zmienne"
  1. foreach($conditions as $key => &$value){
  2. ${'string'.$key} = $value;
  3. }
  4. unset($value);
skowron-line
http://pl.php.net/manual/pl/function.extract.php
bliitz
Cytat(ddiceman @ 13.01.2010, 14:47:24 ) *
no ale jak juz cos, to (za skarby nie moge wymyslec, po co smile.gif ) rozwiazaniem dla Ciebie sa tzw. "zmienne zmienne"


już piszę po co rolleyes.gif , mam taką metodę
  1. /**
  2.   *
  3.   * @param array $post
  4.   * @param array $conditions
  5.   * @return boolean
  6.   */
  7. public function check( $post, array $conditions )
  8. {
  9. $this->post = $post;
  10. $this->conditions = new Validation( $this->post );
  11. $this->conditions->pre_filter( 'trim' );
  12. foreach( $conditions as $k => $v )
  13. {
  14. $this->conditions->add_rules( $k, $v );
  15. }
  16. return $this->conditions->validate();
  17. }


i wywołuje ją w następujący sposób

  1. Parse_Data::instance()->check( $_POST, array( 'queue' => array( 'required', 'numeric' ),
  2. 'spots' => 'required',
  3. 'score' => 'required',
  4. 'lost_spots' => 'required',
  5. 'name' => 'required' ) )


w tym momencie jest błąd, gdyż zamiast postaci:

  1. add_rules( queue, required, numeric );

jest
  1. add_rules( queue, array( required, numeric ) );
ddiceman
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. }

?
bliitz
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. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.