note 112217 added to function.array-column

From: Date: Fri, 17 May 2013 14:49:48 +0000
Subject: note 112217 added to function.array-column
Groups: php.notes 
Request: Send a blank email to php-notes+get-194841@lists.php.net to get a copy of this message
>>> for < PHP5.5 , following function can be used : NEVER edit items of array argument by reference! It WILL damage items of original array, which are referenced outside with another variables like this: $foo = array(10, 20, 30); $bar = &$foo[1]; You should just to collect a new array instead of changing array by reference. So, userland implementation can be such a following: # PHP < 5.5 function array_column(array $input, $columnKey, $indexKey = null) { $result = array(); if (null === $indexKey) { if (null === $columnKey) { // trigger_error('What are you doing? Use array_values() instead!', E_USER_NOTICE); $result = array_values($input); } else { foreach ($input as $row) { $result[] = $row[$columnKey]; } } } else { if (null === $columnKey) { foreach ($input as $row) { $result[$row[$indexKey]] = $row; } } else { foreach ($input as $row) { $result[$row[$indexKey]] = $row[$columnKey]; } } } return $result; } ---- Server IP: 69.147.83.199 Probable Submitter: 92.126.44.202 ---- Manual Page -- http://php.net/manual/en/function.array-column.php Edit -- https://master.php.net/note/edit/112217 Del: integrated -- https://master.php.net/note/delete/112217/integrated Del: useless -- https://master.php.net/note/delete/112217/useless Del: bad code -- https://master.php.net/note/delete/112217/bad+code Del: spam -- https://master.php.net/note/delete/112217/spam Del: non-english -- https://master.php.net/note/delete/112217/non-english Del: in docs -- https://master.php.net/note/delete/112217/in+docs Del: other reasons-- https://master.php.net/note/delete/112217 Reject -- https://master.php.net/note/reject/112217 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194841) next »