将函数应用于数组中的每个元素(O' Reilly cookbook示例)

时间:2015-06-08 13:22:14

标签: arrays function each element apply

用于嵌套数据。

我试过

<?php

$names = array('firstnames' => array("Baba", "Billy"),
'lastnames' => array("O'Riley", "O'Reilly"));

array_walk_recursive($names, function (&value, $key) {
    $value = htmlentities($value, ENT_QOUTES);
})

foreach ($names as $nametypes) {
    foreach ($nametypes as $name) {
        print "$name\n";

    }
}



?>

(来自O&#39; reilly PHP Cookbook第3版)第148页的一个例子

但我不断收到消息

T_String,期待T_Variable

3 个答案:

答案 0 :(得分:2)

使用&$value代替&value 使用ENT_QUOTES而不是ENT_QOUTES

答案 1 :(得分:1)

代码中的错别字(ENT_QUOTES,$ value):

array_walk_recursive($names, function(&$value, $key) {
    $value = htmlentities($value, ENT_QUOTES);
});


foreach ($names as $nametypes) {
    foreach ($nametypes as $name) {
        print "$name\n";

    }
}

答案 2 :(得分:0)

它在工作。

感谢所有

<?php

$names = array('firstnames' => array("Baba", "Billy"),
'lastnames' => array("O'Riley", "O'Reilly"));

array_walk_recursive($names, function (&$value, $key) {
    $value = htmlentities($value, ENT_QUOTES);
});

foreach ($names as $nametypes) {
    foreach ($nametypes as $name) {
        print "$name\n";

    }
}



?>