note 124524 deleted from function.array-column by salathe

From: Date: Fri, 20 Dec 2019 08:53:12 +0000
Subject: note 124524 deleted from function.array-column by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-213559@lists.php.net to get a copy of this message
Note Submitter: unixolutions at gmail dot com ---- For those who may need to return more than one column: function array_columns($input, $columns=array(), $index_key=null) { $result = array(); $idx_list = array(); if (is_scalar($index_key) && !empty($index_key)) { $idx_list = array_column($input, $index_key); } else { $idx_list = array_fill(0, count($input)); } foreach ($columns as $key) { if (is_scalar($key) && !empty($key)) { reset($idx_list); foreach (array_column($input, $key) as $value) { $idx = current($idx_list); if (!isset($result[$idx])) { $result[$idx] = array(); } $result[$idx][$key] = $value; next($idx_list); } } } return $result; }

« previous php.notes (#213559) next »