如何替换多维数组中的所有特殊字符? [英] How can I replace all special characters in a multidimensional array?

查看:55
本文介绍了如何替换多维数组中的所有特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换多维数组中的所有特殊字符:

I want to replace all special characters in my multidimensional array:

array(1) {
  ["one"]=>
  array(1) {
    ["two"]=>
    array(1) {
      [three]=>
      array(1) {
        ["four"]=>
        array(1) {
          ["five"]=>
          array(1) {
            ["Ele╠phant"]=>
            array(1) {
              ["My_Ele╠phant_walks.eps"]=>
              array(3) {
                ["six"]=>
                string(106) "one/two/three/four/five/Ele╠phant/My_Ele╠phant_walks.eps"
                ["seven"]=>
                string(6) "seven"
                ["eight"]=>
                string(19) "eight"
              }
            }
          }
        }
      }
    }
  }
}

所以它应该是这样的:

array(1) {
      ["one"]=>
      array(1) {
        ["two"]=>
        array(1) {
          [three]=>
          array(1) {
            ["four"]=>
            array(1) {
              ["five"]=>
              array(1) {
                ["Ele?phant"]=>
                array(1) {
                  ["My_Ele?phant_walks.eps"]=>
                  array(3) {
                    ["six"]=>
                    string(106) "one/two/three/four/five/Ele?phant/My_Ele?phant_walks.eps"
                    ["seven"]=>
                    string(6) "seven"
                    ["eight"]=>
                    string(19) "eight"
                  }
                }
              }
            }
          }
        }
      }
    }

对于字符串,有一个非常好的工具,它完全符合我的需要:

For strings there is a very nice tool, that does exactly what I need:

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);

我正在为数组寻找相同的东西

And I am looking for the same thing for arrays

推荐答案

function a_iconv(array &$arr) {
  foreach ($arr as $key => $val) {
    if (is_array($val)) {
      a_iconv($arr[$key]);
    } else {
      unset($arr[$key]);
      $arr[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $val);
    }
  }
}

那个函数应该可以.

function a_iconv(array $src) {
  $dst = array();
  foreach ($src as $key => $val) {
    if (is_array($val)) {
      $dst[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = a_iconv($val);
    } else {
      $dst[iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $key)] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $val);
    }
  }
  return $dst;
}

那个应该在不修改原始数组的情况下执行此操作,而是返回新版本.这两个函数都递归地将自己应用于原始数组中的任何数组,将 iconv 应用于任何非数组条目.我没有检查对象,因为我假设您的数组中没有对象.如果恰好是这种情况,请使用 get_object_vars().

That one should do it without modifying the original array, returning the new version instead. Both functions recursively apply themselves on any array in your original array, applying iconv on any non array entry. I didn't checked for objects since I assume you don't have some in your array. Use get_object_vars() if that happens to be the case.

这篇关于如何替换多维数组中的所有特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆