Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 数组_映射在类中不起作用_Php_Arrays_Class_Function_Array Map - Fatal编程技术网

Php 数组_映射在类中不起作用

Php 数组_映射在类中不起作用,php,arrays,class,function,array-map,Php,Arrays,Class,Function,Array Map,我试图创建一个类来处理数组,但似乎无法让array\u map()在其中工作 <?php //Create the test array $array = array(1,2,3,4,5,6,7,8,9,10); //create the test class class test { //variable to save array inside class public $classarray; //function to call array_map function with t

我试图创建一个类来处理数组,但似乎无法让
array\u map()
在其中工作

<?php
//Create the test array
$array = array(1,2,3,4,5,6,7,8,9,10);
//create the test class
class test {
//variable to save array inside class
public $classarray;

//function to call array_map function with the given array
public function adding($data) {
    $this->classarray = array_map($this->dash(), $data);
}

// dash function to add a - to both sides of the number of the input array
public function dash($item) {
    $item2 = '-' . $item . '-';
    return $item2;
}

}
// dumps start array
var_dump($array);
//adds line
echo '<br />';
//creates class object
$test = new test();
//classes function adding
$test->adding($array);
// should output the array with values -1-,-2-,-3-,-4-... 
var_dump($test->classarray);

您以错误的方式将
dash
指定为回调

这不起作用:

$this->classarray = array_map($this->dash(), $data);
这是:

$this->classarray = array_map(array($this, 'dash'), $data);
阅读回调可能采用的不同形式。

它必须阅读

$this->classarray = array_map(array($this, 'dash'), $data);

数组
-是对象实例方法的首选对象。对常规函数的回调被定义为包含函数名的简单字符串(
'functionName'
),而静态方法调用被定义为
array('ClassName,'methodName')
或类似这样的字符串:
'ClassName::methodName'
(从PHP5.2.3开始使用)。

数组映射($this->dash(),$data)
使用0个参数调用
$this->dash()
,并使用返回值作为回调函数应用于数组的每个成员。您需要
array\u映射(array($this,'dash'),$data)

您好,您可以像这样使用

    // Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );

对于多维数组(任何数组):


对于类中的多维数组(任何数组):

$data = array_map(array($this,'decode'), $data);

private function decode($data)
{
    if (is_array($data)) {
        foreach ($data as &$value) {
            if (is_array($value)) {
                $value = $this->decode($value);
            } else {
                $value = html_entity_decode($value);
            }
        }
    } else {
        $data = html_entity_decode($data);
    }
    return $data;
}

可能重复感谢您的快速重播。我成功了,谢谢你的帮助。我只是想知道你是否碰巧有更多关于回调的文章,以及如何指定是否正确?@Justin:看看这里:即使是静态的工作方式也像array_map(@[self,'dash']),你如何用dash函数传递参数?@Nielsapp你不能直接,您必须传入一个可调用函数,该函数将有效地绑定所需的参数,例如
数组映射(函数($item){return$this->dash($item,'additional argument');},$data)
谢谢您的回答。我帮了大忙你还知道关于这个主题的文章吗?再次感谢,
$data = array_map(array($this,'decode'), $data);

private function decode($data)
{
    if (is_array($data)) {
        foreach ($data as &$value) {
            if (is_array($value)) {
                $value = $this->decode($value);
            } else {
                $value = html_entity_decode($value);
            }
        }
    } else {
        $data = html_entity_decode($data);
    }
    return $data;
}