如果我array_walk在类函数内部使用同一个类的另一个函数
class user
{
public function getUserFields($userIdsArray,$fieldsArray)
{
if((isNonEmptyArray($userIdsArray)) && (isNonEmptyArray($fieldsArray)))
{
array_walk($fieldsArray, 'test_print');
}
}
private function test_print($item, $key)
{
//replace the $item if it matches something
}
}
Run Code Online (Sandbox Code Playgroud)
它给我以下错误 -
警告:
array_walk()[function.array-walk]:无法调用test_print()- 函数不存在于......
那么,我$this->test_print()在使用时如何指定array_walk()?
为什么我的示例代码导致第一个字符串仍然有尾随空格?
$a=array('test_data_1 ','test_data_2');
array_walk($a, 'trim');
array_map('trim', $a);
foreach($a AS $b){
var_dump($b);
}
Run Code Online (Sandbox Code Playgroud)
string(12)"test_data_1"string(11)"test_data_2"
$iterator = new ArrayIterator([1, 2]);
array_walk($iterator, function($item) {echo $item . PHP_EOL;});
Run Code Online (Sandbox Code Playgroud)
这段 php 代码在 php 7.3 中输出了项目(1 和 2),但在 php 7.4 中没有输出任何内容。谁能解释 php 7.4 中导致此更改的更改?我在变更日志中找不到任何与此相关的内容。
我在这里浏览了一些答案,但是似乎没有利用这种方法?
我有一组项目,这些项目是对象。该对象可以具有一个键,该键是“子级”,而“子级”是一组对象等。
有没有办法做到这一点?
例:
Array
(
[1] => stdClass Object
(
[id] => 1
[name] => Steve King
[image] => upload/shop/fe7a66254e4249af2b0093efca75a914.jpg
[parent] => 0
[children] => Array
(
)
)
[2] => stdClass Object
(
[id] => 2
[name] => Eden Hall
[image] => upload/shop/064f60a98deba612e437ac549f1dc05d.jpg
[parent] => 0
[children] =>Array
(
[1] => stdClass Object
(
[id] => 1
[name] => Steve King
[image] => upload/shop/fe7a66254e4249af2b0093efca75a914.jpg
[parent] => 0
[children] => Array
(
)
)
)
[3] => stdClass Object …Run Code Online (Sandbox Code Playgroud) 在处理以下代码时,我注意到这declare(strict_types=1)对回调函数的参数没有影响array_walk()
<?php
declare(strict_types=1);
function myCallBack(int $value, $key) {
echo $value;
}
function myFunc(int $value, $key) {
echo $value;
}
$myArr = array("eggs" => 4, "Butter" => 4, "meat" => 4.5);
echo 'myCallBack..';
array_walk($myArr, 'myCallBack'); // Output: 444
echo ' <br />myFunc..';
myFunc(4.2, 'eggs'); // Output:Fatal error: Uncaught TypeError: Argument 1 passed to myFunc() must be of the type integer
?>
Run Code Online (Sandbox Code Playgroud)
我期待 php 抛出异常而不是444因为 [meat] 值$myArr不是整数!
显然 php 忽略了 [meat] in$myArr是float …
我有一段简单的代码,但没有像我预期的那样工作,有人可以解释一下为什么它没有填充字段数组以及如何解决它。
$fields = [];
array_walk($class->properties, function($v, $k) use ($fields) {
$fields[] = $v->name;
});
die(var_dump($fields));
// output is []
Run Code Online (Sandbox Code Playgroud) 我不知道该怎么array_walk($arr, 'intval');做,在以下代码段中已将其注释掉:
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
//array_walk($arr,'intval');
$sum=0;
foreach($arr as $i)
{
$sum = $sum + $i;
}
echo $sum;
?>
Run Code Online (Sandbox Code Playgroud)
如果我使用它或不似乎不会更改输出。
我有一个变量$id = 10,需要在里面使用array_walk().如下:
$id = 10;
array_walk($profile_items, function(&$a) {
$count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
$a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items).exit;
Run Code Online (Sandbox Code Playgroud)
当我在其中使用$id变量时array_walk()显示错误.
消息:未定义的变量:id
有解决方案吗
谢谢你的建议
我需要对orders2数组的索引使用递增值,并且我尝试了以下操作:
$i = 0;
array_walk($arr1, function(&$a) {
$i++;
$a['orders2'] = $i;
});
Run Code Online (Sandbox Code Playgroud)
$i行中说的是未知$i++;。
我知道我可以使用foreach(),但我想知道是否array_walk()有常规循环的行为。任何意见将不胜感激!
array-walk ×9
php ×9
callback ×2
array-map ×1
arrays ×1
codeigniter ×1
iterator ×1
oop ×1
parameters ×1
trim ×1