php - Delete part of index name in multidimensional array -
in php, possible cut |xyz part away key names? array looks this:
array(30) { ["1970-01-01|802"]=> array(4) { ["id"]=> string(3) "176" ["datum"]=> string(10) "1970-01-01" ["title"]=> string(8) "vorschau" ["alias"]=> string(16) "vorschau-v15-176" } ["1970-01-01|842"]=> array(4) { ["id"]=> string(3) "176" ["datum"]=> string(10) "1970-01-01" ["title"]=> string(8) "vorschau" ["alias"]=> string(16) "vorschau-v15-176" } ...
thank help,
toni
for example, might use this:
$newarray = array(); foreach( $oldarray $key => $value ) { $newarray[ substr( $key, 0, 10 ) ] = $value; }
or modify array in-place:
foreach( $somearray $key => $value ) { unset( $somearray[ $key ] ); $somearray[ substr( $key, 0, 10 ) ] = $value; }
both solutions loose value
since keys in source array are
1970-01-01|802 1970-01-01|842
the output array loose array values: both keys mapped single destination key:
1970-01-01
keeping values
if don't want loose values, try this:
$newarray = array(); foreach( $somearray $key => $value ) { $newkey = substr( $key, 0, 10 ); if ( ! isset( $newarray[ $newkey ] )) { $newarray[ $newkey ] = array(); } $newarray[ $newkey ][] = $value; }
result array structure of solution:
array( '1970-01-01' => array( 0 => ..., 1 => ... ), '1970-01-02' => array( 0 => ..., 1 => ..., 2 => ... ), ... );
Comments
Post a Comment