开发者

Applying a function all values in an array

$jobs is an array retrieved f开发者_如何学Crom a DB query. print_r($jobs) shows:

Array
(
    [ID] => 131
    [Title] => -bla-
    [Baseline] => lorem ipsum ...
    [Description] => <ul><li>list 1</li><li>list 2</li></ul>
    [EventID] => 1008
)
Array
(
    [ID] => 132
    [Title] => -bla 2-
    [Baseline] => lorem ipsum lorem ipsum...
    [Description] => <ul><li>list 1</li><li>list 2</li></ul>
    [EventID] => 1009
)

etc ...

Id like to run utf8_encode() on all values of these arrays. I'm not sure if I should use array_map, array_walk_recursive ? The output should not alter the names of the array keys so that I don't need to change anything in my template, so

<h1><?=$j['title']?></h1>

should still work, albeit utf8 encoded.

EDIT: I'm trying the following, no luck

function fix_chars($key, $value)
{
    return utf8_encode($value);
}

array_walk_recursive($jobs, 'fix_chars');


this should work:

<?php
function encode_items(&$item, $key)
{
    $item = utf8_encode($item);
}

array_walk_recursive($jobs, 'encode_items');
?>


Here is an example with array_map():

function utf8_encode_array($array) {
    return array_map('utf8_encode', $array);
}

$encoded_array = array_map('utf8_encode_array', $your_array);

I don't know if there is a performance difference between array_map and array_walk_recursive.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜