note 108194 added to language.types.array
| From: | php_net at mcdragonsoftware dot com | Date: | Fri, 06 Apr 2012 05:55:29 +0000 |
| Subject: | note 108194 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186992@lists.php.net to get a copy of this message | ||
If you're like me you've needed to use index paths (an array as the index for a
multi-dimensional array) and dearly hoped that PHP had native support for it; probably even tried
something like:
<?php
$TEST = array(
array( 'A', 'B', 'C' ),
array( 'X', 'Y', 'Z' ),
array( '1', '2', '3' ),
);
$INDEX = array( 1, 1 );
echo $TEST[$INDEX];
?>
only to receive the Warning: "Illegal offset type...".
I spent the last 3 hours solving that problem, here is my 11 function solution: (these functions
have been tested/verified in PHP 5.3, but not yet in PHP 4)
<?php
//// mda = Multi-Dimensional Array
function mcds_mda_get( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
foreach( (array)$PATH as $el ) {
$SUB = &$SUB[$el];
}
return $SUB;
}
function mcds_mda_set( $ARRAY, $PATH, $value ) {
$SUB = &$ARRAY;
foreach( (array)$PATH as $el ) {
$SUB = &$SUB[$el];
}
$SUB = $value;
return $ARRAY;
}
function mcds_mda_add( $ARRAY, $PATH, $value ) {
$SUB = &$ARRAY;
foreach( (array)$PATH as $el ) {
$SUB = &$SUB[$el];
}
$SUB = array_merge( (array)$SUB, (array)$value );
return $ARRAY;
}
function mcds_mda_delete( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
unset($SUB[$el]);
} else {
$SUB = &$SUB[$el];
}
}
return $ARRAY;
}
//// mdi = Multi-Dimensional Index (for use with mda)
function mcds_mdi_first( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$KEYS = array_keys( $SUB );
$PATH[$i-1] = $KEYS[0];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
function mcds_mdi_prev( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$KEYS = array_keys( $SUB );
$pos = max( array_search( $el, $KEYS ) -1, 0 );
$PATH[$i-1] = $KEYS[$pos];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
function mcds_mdi_next( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$KEYS = array_keys( $SUB );
$pos = min( array_search( $el, $KEYS ) +1, count($KEYS)-1 );
$PATH[$i-1] = $KEYS[$pos];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
function mcds_mdi_last( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$KEYS = array_keys( $SUB );
$PATH[$i-1] = $KEYS[count($KEYS)-1];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
function mcds_mdi_parent( $ARRAY, $PATH ) {
unset($PATH[count($PATH)-1]);
return $PATH;
}
function mcds_mdi_firstChild( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$SUB = &$SUB[$el];
$KEYS = array_keys( $SUB );
$PATH[$i] = $KEYS[0];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
function mcds_mdi_lastChild( $ARRAY, $PATH ) {
$SUB = &$ARRAY;
$i = 0;
foreach( (array)$PATH as $el ) {
$i++;
if( $i == count($PATH) ) {
$SUB = &$SUB[$el];
$KEYS = array_keys( $SUB );
$PATH[$i] = $KEYS[count($KEYS)-1];
} else {
$SUB = &$SUB[$el];
}
}
return $PATH;
}
$TEST = array(
array( 'A', 'B', 'C' ),
array( 'X', 'Y', 'Z' ),
array( '1', '2', '3' ),
);
$INDEX = array( 1, 1 );
$TEST = mcds_mda_set($TEST, $INDEX, "!Y" );
$TEST = mcds_mda_add($TEST, $INDEX, "Y2" );
$TEST = mcds_mda_add($TEST, $INDEX, array("Y3", "Y4") );
$TEST = mcds_mda_delete($TEST, array( 1, 0 ));
// $TEST = mcds_mda_get($TEST, $INDEX);
$TEST = mcds_mda_get($TEST, mcds_mdi_parent($TEST, $INDEX) );
echo "<pre>".print_r( $TEST, true )."</pre>";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.38.194.228
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/108194
Del: integrated -- https://master.php.net/note/delete/108194/integrated
Del: useless -- https://master.php.net/note/delete/108194/useless
Del: bad code -- https://master.php.net/note/delete/108194/bad+code
Del: spam -- https://master.php.net/note/delete/108194/spam
Del: non-english -- https://master.php.net/note/delete/108194/non-english
Del: in docs -- https://master.php.net/note/delete/108194/in+docs
Del: other reasons-- https://master.php.net/note/delete/108194
Reject -- https://master.php.net/note/reject/108194
Search -- https://master.php.net/manage/user-notes.php