MDA (Multi Dimensional Array) access helpers for PHP

0.0.11 2013-09-08 20:50 UTC


MDA (Multi Dimensional Array) access helpers for PHP


$array = array('test'=>array('test2'=>array('test3'=>'value')));

//get a key
$val = mda_get($array,'test.test2.test3'); //returns 'value'

//set a key

//add a key

//get the added key
$val = mda_get($array,'test.test2.test4.0'); //return 'value3'


Keys (Paths)

MDA keys are used in every function. Sometimes they are referred to as "paths" or "path"

  • Dotted notation
  • Notation: mda_get($arr,'index1.index2.index3.index4.0')
  • Array: $arr['index1']['index2']['index3']['index4'][0]
  • Argument notation
  • Notation: mda_get($arr,'index1','index2','index3','index4','0')
  • Array: $arr['index1']['index2']['index3']['index4'][0]
  • Mixed Notation
  • Notation: mda_get($arr,'index1.index2.index3','index4','0')
  • Array: $arr['index1']['index2']['index3']['index4'][0]

(mixed) mda_get(&$arr,$path=null)

Returns the key from $arr

(mixed) mda_set(&$arr,$path=null)

All of the set functions take the value as the last argument Example


(mixed) mda_add(&$arr,$path=null)

Same as set except it adds the value as an anonymous index

//is the same as
$arr['index1']['index2'][] = 'value';

(bool) mda_del(&$arr,$path=null)

Delete a path from array

(mixed) mda_del_value(&$arr,$path=null)

This will delete all values from a path

$arr['index'][0] = 'value';
$arr['index'][1] = 'value';
$count = count($arr['index']); //returns 0

(bool) mda_exists_value(&$arr,$path=null)

This will check if a value exists in a path, same as mda_del_value()

(bool) mda_exists(&$arr,$path)

Checks if a path exists

(array) mda_flatten(&$arr,$keyname)

NOTE: this does not take a path it takes a keyname

  • use mda_get to find the lowest possible path and pass that
  • to this functions eg: $arr = mda_flatten(mda_get($arr,'path1.path2.path3'),'row_id);

(string) implodei($join,$arr=array())

Same prototype as PHPs implode with the enhanced functionality that it will take $join as an array If $join is a string it will pass directly to PHPs implode which is faster Example

$array = array(1,2,3,4,5);
$join = array('/','.',',');
$str = implodei($join,$array); //returns 1/2.3,4,5

NOTE: the last member of the array gets repeated

(mixed) mda_shift($arr)

NOTE: THIS WILL NOT INCREASE THE ARRAYS POINTER (DOES NOT FUNCTION LIKE ARRAY_SHIFT) Use this to shift anonymous arrays only it does not reference the original array Otherwise its the same as PHPs array_shift()

(array) mda_merge($arr,$arr[,$arr...])

Will recursively merge arrays similar to array_merge DOES NOT function like array_merge_recursive which is wrong