BlueShoes Application Framework made with PHP http://www.blueshoes.org/


Packageindex Classtrees Modulegroups Elementlist Report XML Files

File: C:/usr/local/lib/php/blueshoes-4.2/core/util/Bs_Array.class.php
BlueShoes Application Framework - util

Bs_Array

Bs_Object
   |
  +-- Bs_Array

This static class provides many useful array methods that don't come with php.

 

public class Bs_Array extends Bs_Object

This static class provides many useful array methods that don't come with php.
knowledge base:o) some of php's array functions (eg array_pop()) renumber the index of vectors.o) the syntax "$array['someObject']->someFunction()" works now.no dependencies here.

Authors
Version4.0.$id$
Copyrightblueshoes.org

 

Methods inherited from Bs_Object

isex, isexception, tostring, tohtml, persist, unpersist, bs_object, bbsetoutput, bbawake, bbisawake, bbxmsg, bbxfunctionstart, bbxfunctionend, bbxecho, bbxvar, bbxvardump, bbforcetrace, bbbufferstart, bbbufferget, bbbufferendflush, bbbufferendclean

Public Method Summary

array

explode(array $separator, string $string, [ integer $limit ])

similar to php's explode but can explode on multiple strings at once.
int

maxSizeOfLevel(array $array, int $level)

returns the max size of elements of an array
array

merge()

merges 2 arrays.
string

arrayToCode(array &$array, [ string $name ])

takes an array and returns a php code string from it.
mixed

max(array $array, [ string $what ])

returns the highest number that exists in the array.
mixed

min(array $array, [ string $what ])

returns the smallest number that exists in the array.
mixed

randVal(array $array)

Return one of the given elements of the array randomly.
mixed

getLastKey(array $array)

Get the last (highest) key from an array.
bool

setPos(array &$array, mixed $findKey)

sets the pointer to the given key (hash or vector)
int

getPos(array $array, mixed $find, [ string $findKey, string $ignoreCase ])

returns the numeric position of a given key or value in the given array.
string

guessType(array $array)

Guess the array type. associative or not.
void

&padding(array $array, [ string $pad_string, integer $pad_length ], string $pad_type)

This functions pads the input array of strings on the left, the right, or both sides
array

&hashKeysToLower(array &$hashArray)

**********************************************************************
array

&hashKeysToUpper(array &$hashArray)

**********************************************************************
array

splitKeyValue(array $array)

returns the keys and values of the given array separated in 2 arrays.
void

Bs_Array()

Warning: documentation is missing.

Private Method Summary

bool

inArray(string $needle, array $haystack, [ string $ignoreCase, string $ignoreSpaces ])

tells if the value $needle is a value of the array $haystack.
void

uniqueArray( $array)

Warning: documentation is missing.
void

array_rand( $array, $array, $array, $array, $array, $array, $array)

Warning: documentation is missing.
void

array_change_key_case( &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray)

Warning: documentation is missing.

Private Constant Summary

BS_ARRAY_VERSION >>4.0.$x$<< Warning: documentation is missing.

Public Method Details

explode

public array explode( array $separator, string $string, [ integer $limit ] )

  similar to php's explode but can explode on multiple strings at once.
example: you want to explode on ' ' and '-' at once. crap. with thismethod it's a nap.$result = Bs_Array->explode(array(' ', '-'), 'this is your-string');will give you array('this', 'is', 'your', 'string')you can also just do $result = split('[ -]', 'this is your-string');thus i'm not sure if that method is needed at all :/ (thanks to rick).

Parameter
array $separator
string $string
//
integer $limit = >>0<<
(default is 0 which means 'no limit'.)
Returns array


maxSizeOfLevel

public int maxSizeOfLevel( array $array, int $level )

  returns the max size of elements of an array
example:$array = array(0 => array('one', 'two', 'three'),1 => array('one', 'two'),2 => array('one', 'two', 'three', 'four'))maxSizeOfLevel($array, 1) will return 3, that's like a sizeOf().maxSizeOfLevel($array, 2) will return 4.it makes no sense to use $level=1.

Parameter
array $array
int $level
Returns int

Throws int 0 if $array is not an array or empty. @2do currently only supports levels 1 and 2.

merge

public array merge( )

  merges 2 arrays.
this is similar to php's array_merge, but behaves a bit different:php's function sucks in that it fucks up the keys (index) of the arrayssometimes. read the user comments on php.net about it.example:$arrOne = array(''=>'');$arrTwo = array('9'=>'apple', '15'=>'banana', '20'=>'grapefruit');$result = array_merge($arrOne, $arrTwo);now $result is array(''=>'', '0'=>'apple', '1'=>'banana', '2'=>'grapefruit'); in php 4.1.1 (see ecg 'syntax' file)$result = Bs_Array->merge($arrOne, $arrTwo);now $result is array(''=>'', '9'=>'apple', '15'=>'banana', '20'=>'grapefruit');as for array_merge: If the input arrays have the same string keys, then the later value for thatkey will overwrite the previous one.so: if you want to merge 2 numeric arrays and don't care about the index (keys) then you haveto use array_merge().if one of the given params is not an array, it will be ignored.

Returns array

(may be empty)


arrayToCode

public string arrayToCode( array &$array, [ string $name ] )

  takes an array and returns a php code string from it.
example:$array = array('tom', array('fruit'=>"tom's apple is \"green\""), $object, array(15=>20), array());$string = arrayToCode($array, '$foo');now $string looks like$foo['0'] = "tom";$foo['1']['fruit'] = "tom\'s apple is \"green\"";$foo['2'] = '';$foo['3']['15'] = 20;$foo['4'] = array();caution I: objects are shown as empty strings. this is not serialize().caution II: the param $name needs to come with the $ sign. if you use "$name" instead of '$name" you have to escape the $ sign.note: string values are escaped.

Parameter
array &$array
(hash or vector, any construct)
string $name = >>'$array'<<
default is 'array' the name to use as the var name in the returned php code.
Returns string

@throw empty string if param $array is not an array or empty.


max

public mixed max( array $array, [ string $what ] )

  returns the highest number that exists in the array.
only values that are numeric are taken into account.and this is the difference to:http://www.php.net/manual/en/ref.array.php david@preform.dk 01-Feb-1999 12:30use the max function to get the highest value of an array e.g:$maxval = max($array);php's min() and max() treats bool FALSE as the smallest value. we only use numericvalues.note I: this method moves the pointer of the array. you may pass the param by ref.note II: not using php's sort functions here. maybe code may be speed-optimized.note III: if 2 or more numbers are equal (and the max) and you want the 'key' returnedthen only the first key is returned.php offers array_sum() but not this. i guess it will be there somewhen.example:$array = array('en'=>5, 'fr'=>7, 'de'=>3);$maxNum = max($array);$maxLang = max($array, 'key');

Parameter
array $array
vector or hash holding numbers
string $what = >>'value'<<
(one of 'value' (default) or 'key' which means if you want the key or value of the max element returned.)
Returns mixed

(for 'value': double for 'key': string)

Throws bool FALSE (if not an array, or no numeric value at all.)
See Also max()

min

public mixed min( array $array, [ string $what ] )

  returns the smallest number that exists in the array.
see $this->max() there is more information.

Parameter
array $array
vector or hash holding numbers
string $what = >>'value'<<
(one of 'value' (default) or 'key' which means if you want the key or value of the max element returned.)
Returns mixed

(for 'value': double for 'key': string)

Throws bool FALSE (if not an array, or no numeric value at all.)
See Also min()

randVal

public mixed randVal( array $array )

  Return one of the given elements of the array randomly.
note: maybe you better use php's array_rand() for your needs.if no or an empty array was given, an empty string is returned.example:$hello = 'world';$foo = 'bar';$x = $Bs_String::randVal(array(&$hello, &$bar));it's intentional that the param $array is taken by value and notby reference. this way you can use a syntax like the one above inthe example. otherwise you'd have to do$x = $Bs_String::useOneOfArray($array = array($hello, $bar));which is less optimized especially for big array elements.

Parameter
array $array
array you can think of.
Returns mixed

a randomly selected element of the array, by value.


getLastKey

public mixed getLastKey( array $array )

  Get the last (highest) key from an array.
if $array is an associative array, it's the last one. if $array is a 'normal'indexed array, it's an int.note: if you're going to pass the $array param by reference, be aware of thefact that this method moves the pointer to the end.

Parameter
array $array
Returns mixed

the last/highest key, so it's an int or a string.


setPos

public bool setPos( array &$array, mixed $findKey )

  sets the pointer to the given key (hash or vector)
we hope that php comes with such a function sometime.

Parameter
array &$array
(hash or vector)
mixed $findKey
(int or string)
Returns bool

TRUE if key was found, FALSE if not (pos will be after the last element)


getPos

public int getPos( array $array, mixed $find, [ string $findKey, string $ignoreCase ] )

  returns the numeric position of a given key or value in the given array.
the position starts with 0, not 1.example:$array = array('3'=>'viola', '7'=>'sam', '9'=>'rick', '12'=>'fab');$pos = $Bs_Array->getPos(&$array, '7'); //will return 1$pos = $Bs_Array->getPos(&$array, 'RICK', FALSE, TRUE); //will return 2you may pass the $array by ref, but be aware that the pointer will bemoved to somewhere.

Parameter
array $array
mixed $find
(the value to find)
string $findKey = >>TRUE,<<
(where to look, TRUE means key while FALSE means value.)
string $ignoreCase = >>FALSE<<
(if you want to ignore the case for the compare. default is FALSE.)
Returns int

(0-n)

Throws bool FALSE (if not found)

guessType

public string guessType( array $array )

  Guess the array type. associative or not.
arrays in php are great fun. but there's one thing.. these 2 arrays areexactly the same, so once created you cannot tell the difference:array('0'=>'foo', '1'=>'bar')array('foo', 'bar')even the fact that '0' and '1' were written as strings and not 0 and 1as integers does not help; they are converted.it's possible and i can think of cases where someone creates an associativearray with integer keys, they don't need to be strings.if the key is not numeric, it's 100% sure that it's associative. if it's numeric,and the first key has the value 0, we return a guessed zero-based. otherwisea guessed associative-based.note: if you're going to pass the $array param by reference, be aware of thefact that this method moves the pointer to somewhere out in the green ...example:if (substr($Bs_Array->guessType($myArray), 0, 6) == 'vector') echo 'most likely a vector';

Parameter
array $array
Returns string

one of 'hash', 'vector', 'hash_guess', 'vector_guess'
@throw NULL if param is not an array at all, FALSE if array is empty.


&padding

public void &padding( array $array, [ string $pad_string, integer $pad_length ], string $pad_type )

  This functions pads the input array of strings on the left, the right, or both sides
to the specifed padding length. If length is 0 or not set, the size of longest stringin the array will be used as length.Optional argument pad_type can beSTR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH.If pad_type is not specified it is assumed to be STR_PAD_RIGHT.NOTE: You may pass array by reference for direct changes.

Parameter
array $array
above
string $pad_string = >>' '<<
(opt) see above (default ' ')
integer $pad_length = >>0<<
string $pad_type
Warning: documentation is missing.
Returns void


&hashKeysToLower

public array &hashKeysToLower( array &$hashArray )

  **********************************************************************
Transform all keys of a hash array to lower caseNOTE: If 2 keys have the same value after modification, the second overwrites the first.

Parameter
array &$hashArray
Returns array

with all keys to lower case

Deprecated use the new php function array_change_key_case() (php4 cvs only 2002/03/12 --andrej)

&hashKeysToUpper

public array &hashKeysToUpper( array &$hashArray )

  **********************************************************************
Transform all keys of a hash array to upper caseNOTE: If 2 keys have the same value after modification, the second overwrites the first.

Parameter
array &$hashArray
Returns array

with all keys to upper case

Deprecated use the new php function array_change_key_case() (php4 cvs only 2002/03/12 --andrej)

splitKeyValue

public array splitKeyValue( array $array )

  returns the keys and values of the given array separated in 2 arrays.
example:$array = ('name'=>'fab', 'sex'=>'m');list($keys, $values) = splitKeyValue($array);//now $keys = array('name', 'sex') and $values = array('fab', 'm')

Parameter
array $array
(hash or vector)
Returns array

(see example)


Bs_Array

public void Bs_Array( )

 

Warning: documentation is missing.

Returns void


Private Method Details

inArray

private bool inArray( string $needle, array $haystack, [ string $ignoreCase, string $ignoreSpaces ] )

  tells if the value $needle is a value of the array $haystack.
similar to php's in_array() function but this one is used for stringsonly and is able to ignore case and/or spaces. the 'strict' param is of nouse here. if you don't need ignorecase or ignorespaces then you are betteroff with php's function, which must be faster.moves the pointer of $haystack, but does not change values. so you maypass the array by reference.example:$array = array('abc', ' HELLO ');inArray('hello'); returns TRUE.

Parameter
string $needle
array $haystack
string $ignoreCase = >>TRUE,<<
(case insensitive search.)
string $ignoreSpaces = >>TRUE<<
(spaces (etc) in $haystack and $needle are ignored using trim().)
Returns bool


uniqueArray

private void uniqueArray( $array )

 

Warning: documentation is missing.

Parameter
$array
Warning: documentation is missing.
Returns void


array_rand

private void array_rand( $array, $array, $array, $array, $array, $array, $array )

 

Warning: documentation is missing.

Parameter
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
$array
Warning: documentation is missing.
Returns void


array_change_key_case

private void array_change_key_case( &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray, &$hashArray )

 

Warning: documentation is missing.

Parameter
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
&$hashArray
Warning: documentation is missing.
Returns void


Private Constant Details

BS_ARRAY_VERSION

define( BS_ARRAY_VERSION, >>4.0.$x$<< )
Case: default: case sensitive




Packageindex Classtrees Modulegroups Elementlist Report XML Files
PHPDoc 1.0beta