note 93762 added to language.references.whatdo

From: Date: Sat, 26 Sep 2009 22:29:21 +0000
Subject: note 93762 added to language.references.whatdo
Groups: php.notes 
Request: Send a blank email to php-notes+get-161100@lists.php.net to get a copy of this message
An interesting if offbeat use for references: Creating an array with an arbitrary number of dimensions. For example, a function that takes the result set from a database and produces a multidimensional array keyed according to one (or more) columns, which might be useful if you want your result set to be accessible in a hierarchial manner, or even if you just want your results keyed by the values of each row's primary/unique key fields. <?php function array_key_by($data, $keys, $dupl = false) /* * $data - Multidimensional array to be keyed * $keys - List containing the index/key(s) to use. * $dupl - How to handle rows containing the same values. TRUE stores it as an Array, FALSE overwrites the previous row. * * Returns a multidimensional array indexed by $keys, or NULL if error. * The number of dimensions is equal to the number of $keys provided (+1 if $dupl=TRUE). */ { // Sanity check if (!is_array($data)) return null; // Allow passing single key as a scalar if (is_string($keys) or is_integer($keys)) $keys = Array($keys); elseif (!is_array($keys)) return null; // Our output array $out = Array(); // Loop through each row of our input $data foreach($data as $cx => $row) if (is_array($row)) { // Loop through our $keys foreach($keys as $key) { $value = $row[$key]; if (!isset($last)) // First $key only { if (!isset($out[$value])) $out[$value] = Array(); $last =& $out; // Bind $last to $out } else // Second and subsequent $key.... { if (!isset($last[$value])) $last[$value] = Array(); } // Bind $last to one dimension 'deeper'. // First lap: was &$out, now &$out[...] // Second lap: was &$out[...], now &$out[...][...] // Third lap: was &$out[...][...], now &$out[...][...][...] // (etc.) $last =& $last[$value]; } if (isset($last)) { // At this point, copy the $row into our output array if ($dupl) $last[$cx] = $row; // Keep previous else $last = $row; // Overwrite previous } unset($last); // Break the reference } else return NULL; // Done return $out; } // A sample result set to test the function with $data = Array(Array('name' => 'row 1', 'foo' => 'foo_a', 'bar' => 'bar_a', 'baz' => 'baz_a'), Array('name' => 'row 2', 'foo' => 'foo_a', 'bar' => 'bar_a', 'baz' => 'baz_b'), Array('name' => 'row 3', 'foo' => 'foo_a', 'bar' => 'bar_b', 'baz' => 'baz_c'), Array('name' => 'row 4', 'foo' => 'foo_b', 'bar' => 'bar_c', 'baz' => 'baz_d') ); // First, let's key it by one column (result: two-dimensional array) print_r(array_key_by($data, 'baz')); // Or, key it by two columns (result: 3-dimensional array) print_r(array_key_by($data, Array('baz', 'bar'))); // We could also key it by three columns (result: 4-dimensional array) print_r(array_key_by($data, Array('baz', 'bar', 'foo'))); ?> ---- Server IP: 209.41.74.194 Probable Submitter: 67.237.228.46 ---- Manual Page -- http://www.php.net/manual/en/language.references.whatdo.php Edit -- https://master.php.net/note/edit/93762 Del: integrated -- https://master.php.net/note/delete/93762/integrated Del: useless -- https://master.php.net/note/delete/93762/useless Del: bad code -- https://master.php.net/note/delete/93762/bad+code Del: spam -- https://master.php.net/note/delete/93762/spam Del: non-english -- https://master.php.net/note/delete/93762/non-english Del: in docs -- https://master.php.net/note/delete/93762/in+docs Del: other reasons-- https://master.php.net/note/delete/93762 Reject -- https://master.php.net/note/reject/93762 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161100) next »