WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder



Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Transpose of an array
Categories : PHP, Arrays Click here to Update Your Picture
Sumit Agarwal
Date : Nov 17th 2007
Grade : 5 of 5 (graded 1 times)
Viewed : 16104
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sumit Agarwal
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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'));

?>



Function to create a separated list
Categories : PHP, Arrays, Strings
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
translate.php - Assocciative array example, passing a reference to a function.
Categories : PHP, Arrays, Languages, Variables
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
CSS style switcher
Categories : PHP, CSS, HTML and PHP, Arrays, Sessions
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
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
This gets the http response headers for a given url and returns them in an assoc array. i.e. to test if a url exists: $array = get_http_headers($url); if($array[result]=200) { }
Categories : HTTP, Arrays, PHP
A function which places the path and name of all subdirectories into an array
Categories : PHP, Filesystem, Arrays, Directories
Array Insertion
Categories : PHP, PHP Classes, Arrays
BBCode Formatting String
Categories : PHP, HTML, Regexps, Arrays
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
Selecting a random line from a text file
Categories : PHP, Filesystem, Arrays, Beginner Guides
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL