note 97864 added to language.types.array
| From: | dtomasiewicz at gmail dot com | Date: | Wed, 12 May 2010 17:42:57 +0000 |
| Subject: | note 97864 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169192@lists.php.net to get a copy of this message | ||
<?php
/**
* Functions for examining and manipulating matrices (n-dimensional arrays) of data
* with string dot-separated paths. For example, you might do this with multidimensional
* array:
* $var = $array['someKey']['cats']['dogs']['potato'];
*
* Accomplishing this can be a nightmare if you don't know the depth of the path or the array
* is of a variable dimension.
*
* You can accomplish the same by using $array as a Matrix:
* $array = new Matrix($array);
* $var = $array->get('someKey.cats.dogs.potato);
*
* @author Daniel Tomasiewicz <www.fourstaples.com>
*/
class Matrix {
private $data;
public function __construct(array $data = array()) {
$this->data = $data;
}
/**
* Gets the value at the specified path.
*/
public function get($path = null) {
if($path === null) {
return $this->data;
}
$segs = explode('.', $path);
$target =& $this->data;
for($i = 0; $i < count($segs)-1; $i++) {
if(isset($target[$segs[$i]]) && is_array($target[$segs[$i]])) {
$target =& $target[$segs[$i]];
} else {
return null;
}
}
if(isset($target[$segs[count($segs)-1]])) {
return $target[$segs[count($segs)-1]];
} else {
return null;
}
}
/**
* Sets a value to a specified path. If the provided value is
* null, the existing value at the path will be unset.
*/
public function set($path, $value = null) {
if(is_array($path)) {
foreach($path as $p => $v) {
$this->set($p, $v);
}
} else {
$segs = explode('.', $path);
$target =& $this->data;
for($i = 0; $i < count($segs)-1; $i++) {
if(!isset($target[$segs[$i]])) {
$target[$segs[$i]] = array();
}
$target =& $target[$segs[$i]];
}
if($segs[count($segs)-1] == '*') {
foreach($target as $key => $value) {
$target[$key];
}
} elseif($value === null && isset($target[$segs[count($segs)-1]])) {
unset($target[$segs[count($segs)-1]]);
} else {
$target[$segs[count($segs)-1]] = $value;
}
}
}
/**
* Returns a flattened version of the data (one-dimensional array
* with dot-separated paths as its keys).
*/
public function flatten($path = null) {
$data = $this->get($path);
if($path === null) {
$path = '';
} else {
$path .= '.';
}
$flat = array();
foreach($data as $key => $value) {
if(is_array($value)) {
$flat += $this->flatten($path.$key);
} else {
$flat[$path.$key] = $value;
}
}
return $flat;
}
/**
* Expands a flattened array to an n-dimensional matrix.
*/
public static function expand($flat) {
$matrix = new Matrix();
foreach($flat as $key => $value) {
$matrix->set($key, $value);
}
return $matrix;
}
}
----
Server IP: 69.147.83.197
Probable Submitter: 24.78.148.130
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/97864
Del: integrated -- https://master.php.net/note/delete/97864/integrated
Del: useless -- https://master.php.net/note/delete/97864/useless
Del: bad code -- https://master.php.net/note/delete/97864/bad+code
Del: spam -- https://master.php.net/note/delete/97864/spam
Del: non-english -- https://master.php.net/note/delete/97864/non-english
Del: in docs -- https://master.php.net/note/delete/97864/in+docs
Del: other reasons-- https://master.php.net/note/delete/97864
Reject -- https://master.php.net/note/reject/97864
Search -- https://master.php.net/manage/user-notes.php