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

?>



PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B.
Categories : PHP, Arrays, Algorithms
Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript.
Categories : PHP, Java Script, DOM XML, XML, Arrays
How to display any array in several rows and columns of a table. Not just in one column or in alternate rows. This example shows a nice color table generated with PHP, but can be used with any array values(e.g. Database)
Categories : Arrays, PHP, Miscellaneous, Beginner Guides, Graphics
Select with current month
Categories : PHP, HTML and PHP, Date Time, Arrays
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
How to pass an array to a function, or how to define a function wich recieves an array.
Categories : Variables, PHP, Arrays
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
This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query
Categories : Databases, PHP, MySQL, Arrays
Array values from javascript to php
Categories : PHP, Java Script, Arrays
Filling an array with files from any given directory. This example is for the current PHP script's directory.
Categories : PHP, Arrays, Filesystem
Get TemplateMonster data
Categories : Arrays, Ecommerce, PHP, Strings