Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.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/3/arrays/12.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 - Fatal编程技术网

如何使用PHP调用数组值内的函数

如何使用PHP调用数组值内的函数,php,arrays,Php,Arrays,实际上,我需要调用数组值中的函数 这是我的职责 public function RelationName() { return $this->name .'--'. $this->relationship; } 我需要在呈现相同类文件的另一个函数中调用上述函数 这是我的另一个功能: public static function getClaimerNameList($cat_id, $subcat_id) { $out = [

实际上,我需要调用数组值中的函数

这是我的职责

public function RelationName()
    {
        return $this->name .'--'. $this->relationship;
    }
我需要在呈现相同类文件的另一个函数中调用上述函数

这是我的另一个功能:

public static function getClaimerNameList($cat_id, $subcat_id)
    {
        $out = [];

        $emp_code = Employee::find()
                ->where(['importcompany_id' => $cat_id])
                ->andWhere(['id' => $subcat_id])
                ->all();

        foreach ($emp_code as $dat => $datas) {
            $out[] = ['id' => $datas['id'], 'name' => $datas['name'] ];

            if($dat == 0){
                    $aux = $datas['id'];
                }

            ($datas['id'] == $subcat_id) ? $selected = $subcat_id : $selected = $aux;

        }
        return $output = [
            'output' => $out,
            'selected' => $selected
        ];
    }
实际上我需要在这里调用函数

foreach ($emp_code as $dat => $datas) {
                $out[] = ['id' => $datas['id'], 'name' => $datas['name'] ];
}
我需要调用函数
getRelationName()

没有使用函数,我得到的输出像

id name
1  raja
2  ram
但我不想这样输出,如果我使用一个函数,我会得到如下输出

id name
1   raja-father
2   ram-son

帮我解决这个问题

你需要这样称呼它

foreach ($emp_code as $dat => $datas) 
{
  $out[] = ['id' => $datas['id'], 'name' => $datas->RelationName() ];

  //more code...
}

RelationName()
需要位于
Employee
类中,并且它不应该是静态的

您的意思是$name=RelationName()$out[]=['id'=>$datas['id'],'name'=>$name];是的,我已经试过了,但是我得到了错误发布您的尝试并提到错误详细信息将此错误调用设置为未定义的函数RelationName如果我像这样使用$name=$this->RelationName(),那么当不在对象上下文中时,我得到的错误是使用$this'Am获取此错误PHP致命错误';yii\base\ErrorException';带有消息';当不在对象上下文中时使用$this';请注意,它不是
$this->RelationName()
,而是
$datas->RelationName()
。另外,您确定
RelationName()
是在
Employee
中定义的吗?我只在$datas->RelationName()中使用过,我只在Employee中定义过,但仍然有错误。您绝对确定您的RelationName()函数没有声明为静态函数吗?这不在你的问题中,但这类错误指出了这一点。好吧,这就是问题所在,它不应该是静态的。