note 107440 added to function.range

From: Date: Thu, 09 Feb 2012 11:30:31 +0000
Subject: note 107440 added to function.range
Groups: php.notes 
Request: Send a blank email to php-notes+get-185935@lists.php.net to get a copy of this message
Ever wanted to generate an array with a range of column names for use in Excel file related parsing? I've wrote a function that starts at the A column and adds column names up until the column you specified. <?php /** * This function creates an array with column names up until the column * you specified. */ function createColumnsArray($end_column, $first_letters = '') { $columns = array(); $length = strlen($end_column); $letters = range('A', 'Z'); // Iterate over 26 letters. foreach ($letters as $letter) { // Paste the $first_letters before the next. $column = $first_letters . $letter; // Add the column to the final array. $columns[] = $column; // If it was the end column that was added, return the columns. if ($column == $end_column) return $columns; } // Add the column children. foreach ($columns as $column) { // Don't itterate if the $end_column was already set in a previous itteration. // Stop iterating if you've reached the maximum character length. if (!in_array($end_column, $columns) && strlen($column) < $length) { $new_columns = createColumnsArray($end_column, $column); // Merge the new columns which were created with the final columns array. $columns = array_merge($columns, $new_columns); } } return $columns; } ?> Usage: <?php // Return an array with all column names from A until and with BI. createColumnsArray('BI'); ?> ---- Server IP: 194.50.97.14 Probable Submitter: 194.78.7.202 ---- Manual Page -- http://www.php.net/manual/en/function.range.php Edit -- https://master.php.net/note/edit/107440 Del: integrated -- https://master.php.net/note/delete/107440/integrated Del: useless -- https://master.php.net/note/delete/107440/useless Del: bad code -- https://master.php.net/note/delete/107440/bad+code Del: spam -- https://master.php.net/note/delete/107440/spam Del: non-english -- https://master.php.net/note/delete/107440/non-english Del: in docs -- https://master.php.net/note/delete/107440/in+docs Del: other reasons-- https://master.php.net/note/delete/107440 Reject -- https://master.php.net/note/reject/107440 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185935) next »