PHP Classes

File: biblio_array

Recommend this page to a friend!
  Classes of Julien PACHET   Form Classic   biblio_array   Download  
File: biblio_array
Role: Auxiliary script
Content type: text/plain
Description: Functions to manipulate arrays
Class: Form Classic
Generate the HTML for forms with common inputs
Author: By
Last change:
Date: 20 years ago
Size: 5,826 bytes
 

Contents

Class file image Download
<?php

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FILE NAME : Biblio_array.inc.php //
// LANGUAGE : PHP //
// AUTHOR : Julien PACHET //
// EMAIL : j|u|l|i|e|n| [@] |p|a|c|h|e|t.c|o|m //
// VERSION : 1.11 //
// CREATION : 05/11/2003 //
// LICENCE : GNU/GPL //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// What the library does: //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// * Help manipulate arrays //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Changelog: //
// ---------- //
// Date Version Actions //
// ------------------------------------------------------------------------------------------------------ //
// 05/11/2003 1.0 Tested & Final version //
// 29/02/2004 1.1 array_2D_transpose //
// now can transpose associative arrays //
// 01/03/2004 1.11 optimizations //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Need to work: //
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// * other file : none //
////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Declaration //
// ----------- //
// array_1D($vect) //
// array_2D($array) //
// array_assoc_display($array) //
// array_2D_transpose($tab) //
// array_2D_sort($tab,$column_name,$sens="ASC") //
//////////////////////////////////////////////////////////////////////

function array_1D($vect) {
   
/**
    * array_1D() : return a displayable version of an 1D array
    *
    * @param $vect
    * @return
    */
   
if (count($vect)>0)
        return
"[ ".implode(",",$vect)." ]";
    else
        return
"[ ]";
}

function
array_2D($array) {
   
/**
    * array_2D() : return a displayable version of an 2D array
    *
    * @param $array
    * @return
    */
   
for ($res.=$i=0;$i<count($array);$i++)
       
$res.="\t".array_1D($array[$i])."<br>\n";
    return
"[ <br>\n$res ]<br>\n";
}

function
array_assoc_display($array) {
   
/**
    * array_assoc_display() : display an associated array
    *
    * @param $array
    * @return
    */
   
echo "<pre>\n";
   
print_r($array);
    echo
"</pre>\n";
}

function
array_2D_transpose($tab) {
   
/**
    * array_2D_transpose() : mathematical transpose of the 2D array
    *
    * @param $tab
    * @return
    */
   
$tab1=array_keys($tab);
   
$tab2=array_keys($tab[$tab1[0]]);
   
$l1=count($tab1);
   
$l2=count($tab2);
    for (
$x=0;$x<$l1;$x++)
        for (
$y=0;$y<$l2;$y++)
           
$res[$tab2[$y]][$tab1[$x]]=$tab[$tab1[$x]][$tab2[$y]];
    return
$res;
}

function
array_2D_sort($tab,$column_name,$sens="ASC") {
   
/**
    * array_2D_sort() : sort a 2D array by the $column_name $column
    *
    * @param $tab
    * @param $column_name
    * @param $sens
    * @return
    */
   
$res=array();
    while (
count($tab)>0) {
        for (
$i=0;$i<count($tab);$i++)
           
$column[$i]=$tab[$i][$column_name];
        for (
$pos=0,$i=0;$i<count($tab);$i++)
           
$pos=($sens=="ASC")?(($tab[$i][$column_name]<$tab[$pos][$column_name])?$i:$pos):(($tab[$i][$column_name]>$tab[$pos][$column_name])?$i:$pos);
       
$res[]=$tab[$pos];
       
array_splice($tab,$pos,1);
    }
    return
$res;
}
?>