开发者

i want to use iconv function for array?

this is my array:

$array = array(
                'name' => $burcname . ' Günlük yorumum',
                'link' => 'http://apps.facebook.com/gunlukburcpaylas/sonuc.php?burc='. $burc,
                'description' => $burcname . ' Günlük yorumu. Sizde her gün profilinizde günlük burç yorumunuz için uygulamaya katılın..',
                'message' => $aciklama,
                'picture' => $picture
                );

iconv as: $example = iconv('UTF-8','ISO-8859-9',$array);

But this is array. and dont work. 开发者_运维百科What can i do?


You will need to iterate the array contents, or use a function like array_walk.

Foreach loop (untested)

foreach(array_keys($array) as $key){
    $array[$key] = iconv('UTF-8','ISO-8859-9', $array[$key]);
}  

The reason you need to use array_keys in this example is because a standard foreach loop with foreach($array as $key => $value) or foreach($array as $value) modifications made to $value are not preserved.

Using array_walk (untested)

function convert(&$value, $key){
    $value = iconv('UTF-8','ISO-8859-9', $value);
}
array_walk($array, 'convert');

If you are using PHP > 5.3 then you can use a lambda function instead.


array_walk_deep function (tested)

$array = array("a",
    array("b",
      array("c",
        "d"
      )
    )
);

function array_walk_deep(&$items,$func){
    foreach ($items as &$item) {
        if(is_array($item))
          array_walk_deep($item,$func);
        else
          $item = $func($item);
    }
}

array_walk_deep($array, 'strtoupper');

print_r($array);


Another solution would be to return the results from the foreach by reference instead of by value:

foreach( $array as &$value ) {
  $value = iconv( 'UTF-8','ISO-8859-9', $value );
}

The '&' in front of the value variable lets you use the actual array value instead of a copy. :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜