PHP:重命名多维数组的键

时间:2021-08-04 02:41:40

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

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.

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

I hope I made some sense, thanks in advance!

我希望我有所作为,提前谢谢!

4 个解决方案

#1


1  

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

#2


1  

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

#3


0  

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

#4


0  

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

#1


1  

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

#2


1  

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

#3


0  

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

#4


0  

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