简体   繁体   中英

How can I replace all special characters in a multidimensional array?

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"
              }
            }
          }
        }
      }
    }
  }
}

So as a result it should look like this:

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

You could convert the array to a json string, convert the special characters in this string, and convert the json string back to an array.

$json = json_encode($array);
$json = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $json);
$array = json_decode($json, true);
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);
    }
  }
}

That function should do it.

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

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.

function custonDecode($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[$key] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $val);
     }
  }
  return $dst;
}

 print_r(custonDecode($yourArray));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM