簡體   English   中英

如何使用PHP在數組值內調用函數

[英]How to Call a function inside an array value using PHP

實際上,我需要在數組值內調用一個函數。

這是我的職責

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'] ];
}

instead '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類中,並且不應該是靜態的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM