note 32420 deleted from function.array-push by didou

From: Date: Tue, 27 May 2003 12:25:43 +0000
Subject: note 32420 deleted from function.array-push by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-49088@lists.php.net to get a copy of this message
Note Submitter: kerxen@caramail.com ---- YOU NEED A FUNCTION TO EXTRACT ONE COLUMN OF A TABLE /** ************************** * Document::tableColumn() * * { This function sends back a column of the table or the table itself if it has only one column. * Input : $table, a table at 1 ou 2 dimensions or more * $numeroCol, the number of the column. * Output : $output : the column $numero of the table or the table. * } * ********** **********/ function tableColumn ( $table, $numeroCol) { $output = array(); reset ($table) ; if (!is_integer($numeroCol)) $numeroCol=0; if (count($table)==0) return "UNDEFINED"; else { foreach ($table as $ligne) // $ligne est un tableau (si $table est en 2D) ou un string ($table en 1D) { if (gettype($ligne)!="array") { if (strlen($ligne)!=0) array_push ($output, $ligne) ; // on met le string dans $output else array_push ($output, " ") ; // on met une chaine " " dans $output } else { if (strlen($ligne[$numeroCol])!=0) array_push ($output, $ligne[$numeroCol]) ; // on insere la colonne $numero de la ligne dans $output else array_push ($output, " ") ; // on met une chaine " " dans $output } } return $output; } } /***************************/ SYNOPTIC : how to use it : /***************************/ try the exemple below : $stack = array ("orange", "banane"); array_push ($stack, "pomme", "bleuet"); $stock = array ($stack,$stack); print_r ($stock); echo ' '; print_r ($stack); echo ' '; print_r (tableColumn ($stock,0)) ; echo ' '; print_r (tableColumn ($stock,1)) ; echo ' '; print_r (tableColumn ($stock,2)) ; echo ' '; print_r (tableColumn ($stock,3)) ; echo ' '; print_r (tableColumn ($stack,0)) ; echo ' '; print_r (tableColumn ($stack,1)) ; echo ' '; YOU WILL GET THIS : Array ( [0] => Array ( [0] => orange [1] => banane [2] => pomme [3] => bleuet ) [1] => Array ( [0] => orange [1] => banane [2] => pomme [3] => bleuet ) ) Array ( [0] => orange [1] => banane [2] => pomme [3] => bleuet ) Array ( [0] => orange [1] => orange ) Array ( [0] => banane [1] => banane ) Array ( [0] => pomme [1] => pomme ) Array ( [0] => bleuet [1] => bleuet ) Array ( [0] => orange [1] => banane [2] => pomme [3] => bleuet ) Array ( [0] => orange [1] => banane [2] => pomme [3] => bleuet ) /**************************/ Good Luck Eddy Cingala

« previous php.notes (#49088) next »