note 112531 deleted from function.array-column by sobak

From: Date: Tue, 01 Jul 2014 17:38:37 +0000
Subject: note 112531 deleted from function.array-column by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199803@lists.php.net to get a copy of this message
Note Submitter: jan venekamp ---- Implementation for PHP < 5.5.0 Following: https://wiki.php.net/rfc/array_column <?php // PHP < 5.5.0 if (!function_exists('array_column')) { function array_column($input, $column_key, $index_key = null) { if ($index_key !== null) { // Collect the keys $keys = array(); $i = 0; // Counter for numerical keys when key does not exist foreach ($input as $row) { if (array_key_exists($index_key, $row)) { // Update counter for numerical keys if (is_numeric($row[$index_key]) || is_bool($row[$index_key])) { $i = max($i, (int) $row[$index_key] + 1); } // Get the key from a single column of the array $keys[] = $row[$index_key]; } else { // The key does not exist, use numerical indexing $keys[] = $i++; } } } if ($column_key !== null) { // Collect the values $values = array(); $i = 0; // Counter for removing keys foreach ($input as $row) { if (array_key_exists($column_key, $row)) { // Get the values from a single column of the input array $values[] = $row[$column_key]; $i++; } elseif (isset($keys)) { // Values does not exist, also drop the key for it array_splice($keys, $i, 1); } } } else { // Get the full arrays $values = array_values($input); } if ($index_key !== null) { return array_combine($keys, $values); } return $values; } } ?>

« previous php.notes (#199803) next »