|
|
|
Recently I wanted to find the transpose of an array. But there is no inbuilt function for doing that. This code can find the transpose of a 2D array. If an optional second parameter is passed, then the function returns that row of the new matrix.
| <?php
function array_transpose($array, $selectKey = false) {
if (!is_array($array)) return false;
$return = array();
foreach($array as $key => $value) {
if (!is_array($value)) return $array;
if ($selectKey) {
if (isset($value[$selectKey])) $return[] = $value[$selectKey];
} else {
foreach ($value as $key2 => $value2) {
$return[$key2][$key] = $value2;
}
}
}
return $return;
}
$fruits = array(
array('id' => 1, 'name' => 'Apple', 'color' => 'Red'),
array('id' => 2, 'name' => 'Orange', 'color' => 'Orange'),
array('id' => 3, 'name' => 'Mango', 'color' => 'Yellow')
);
print_r(array_transpose($fruits));
print_r(array_transpose($fruits, 'name'));
?> | | |
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read. Categories : PHP, Rich Site Summary (RSS), Arrays | | | Looping through two arrays Categories : PHP, Databases, Arrays | | | Simple way of scaling any image to fit either given width or height. Categories : PHP, Graphics, Arrays | | | PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside. Categories : PHP, Arrays, Variables | | | A database abstraction layer for the PHP Oracle 8 module (available from PHP 3.0.5). It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface. Categories : Databases, Oracle, PHP, Arrays, Variables | | | array -- Create an array Categories : PHP, PHP Functions, Arrays | | | CSS style switcher Categories : PHP, CSS, HTML and PHP, Arrays, Sessions | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | How to get the source of a site into an array. Categories : Arrays, HTML, PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | |
|
|
|