开发者

PHP: rename multidimensional array's keys

开发者 https://www.devze.com 2023-01-04 06:50 出处:网络
I have a multidimensional array with strings as keys. I want to perform a function (to manipulate the strings) on those keys and then write to a new array (i.e. leave the original array unchanged).

I have a multidimensional array with strings as keys. I want to perform a function (to manipulate the strings) on those keys and then write to a new array (i.e. leave the original array unchanged).

Example:

$oldArr = array(
    "foo_old" => array("moo_old" => 1234, "woo_old" => 5678);
    "bar_old" => array("car_old" => 4321, "tar_old" => 8765);
开发者_JAVA百科);

Becomes:

$newArr = array(
    "foo_new" => array("moo_new" => 1234, "woo_new" => 5678);
    "bar_new" => array("car_new" => 4321, "tar_new" => 8765);
);

This is just an example, the actual array has more levels/dimensions. Oh and my function doesn't replace "_old" with "_new", again, just an example.

I hope I made some sense, thanks in advance!


Edit: I added a function for printing out the changed array. You may include the code on a website and it will show the result. New edited code:

// array initialisation
oldArr = array();
$subArr1 = array();
$subArr2 = array();

$subArr1["moo_old"]=1234;
$subArr1["woo_old"]=5678;
$subArr2["car_old"]=4321;
$subArr2["tar_old"]=8765;
$oldArr["foo_old"]=$subArr1;
$oldArr["bar_old"]=$subArr2;

$oldArr;  // make a copy of the array

// function which replaces recursivly the keys of the array 
function renameArrayKeys( $oldArr ) {

 $copyArr = $oldArr;

    if( is_array( $oldArr) && count( $oldArr ) ) {
        foreach ( $oldArr as $k => $v ) {

            unset($copyArr[$k]); // removes old entries
            $newKey = str_replace( '_old', '_new', $k );

            if( is_array( $v ) ) {
    $copyArr[ $newKey ] = renameArrayKeys( $v );
            }
            else {
             $copyArr[ $newKey ] = $v;
            }
        }
        return $copyArr;
    }
}

// prints out the keys and values of the changed array
function printout($arr ){
 foreach ($arr as $k => $val ) {
  echo $k."=>".$val." | ";
  if( is_array( $val ) ) {
             printout( $val );
  }
 }
}

// calls the above functions
$changedArr = renameArrayKeys($oldArr);
printout($changedArr);


I'm probably slightly late, but recursion is the way forward with this!

$replace_from = "_old"; //can also be array i.e. array("foo_old", "bar_old")
$replace_to = "_new"; //can also be an array i.e. array("foo_new", "bar_new")

$oldArr = array(
    "foo_old" => array("moo_old" => 1234, "woo_old" => 5678),
    "bar_old" => array("car_old" => 4321, "tar_old" => 8765),
);

function replace($arr){
    global $replace_from, $replace_to;
    $newArr = array();
    foreach($arr as $key => $value){
        $newArr[str_replace($replace_from,$replace_to,$key)] = (is_array($value)) ? replace($value) : $value;
    }
    return $newArr;
}

print_r (replace($oldArr));


Something like this:

function renameKeys( $arr ) 
{
    if( is_array( $arr ) && count( $arr ) ) {
            foreach ( $arr as $k => $v ) {
                $nk = str_replace( '_old', '_new', $k );
                if( is_array( $v ) ) {
                    $v = renameKeys( $v );
                }
                $arr[ $nk ] = $v;
                unset( $arr[$k] );
            }
    }
    return $arr;
}
$oldArr = array(
    "foo_old" => array("moo_old" => 1234, "woo_old" => 5678) ,
        "bar_old" => array("car_old" => 4321, "tar_old" => 8765)
        );

$nArr = renameKeys( $oldArr );
print_r( $nArr );


Closure version. This doesn't mess up the namespace.

<?php

$from = '_old';
$to   = '_new';

$old_arr = array(
    'foo_old' => array('moo_old' => 1234, 'woo_old' => 5678),
    'bar_old' => array('car_old' => 4321, 'tar_old' => 8765),
);

$func = function ($arr) use (&$func, $from, $to) {
    $new_arr = array();
    foreach($arr as $k => $v){
        $new_arr[str_replace($from, $to, $k)] = is_array($v) ? $func($v) : $v;
    }
    return $new_arr;
};

print_r($func($old_arr));
0

精彩评论

暂无评论...
验证码 换一张
取 消