PHP:重命名多维数组的键

时间:2010-06-23 14:03:41

标签: php arrays multidimensional-array

我有一个多维数组,字符串作为键。我想在这些键上执行一个函数(操作字符串),然后写入一个新数组(即保持原始数组不变)。

示例:

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

变为:

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

这只是一个例子,实际的数组有更多的级别/维度。哦,我的功能不会将“_old”替换为“_new”,再次只是一个例子。

我希望我有所帮助,先谢谢!

4 个答案:

答案 0 :(得分:1)

编辑:我添加了一个用于打印已更改数组的函数。您可以在网站上包含代码,它会显示结果。新编辑的代码:

// 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);

答案 1 :(得分:1)

我可能会稍晚一些,但递归是前进的方法!

$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));

答案 2 :(得分:0)

这样的事情:

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 );

答案 3 :(得分:0)

关闭版本。这不会搞乱命名空间。

<?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));