如何在代码点火器中访问另一个函数中的多变量返回?

时间:2017-05-23 03:49:14

标签: php codeigniter

如何在另一个功能代码点火器中访问多变量返回?

我的代码是这样的:

function extract_fitur($string, $judul){

    $data_total_kalimat=array();
    $data_kalimat=array();
    $data_posisi_kalimat=array();
    $data_kata3=array();
    foreach ($string as $key => $value) {
        $kalimat=$value;
        $total_kalimat=count($value);
        $data_posisi=array();
        $data_kata2=array();

        foreach ($value as $key2 =>$value2) {
            $data_kata=array();
            $pecah_kata=explode(" ", $value2);  
            foreach ($pecah_kata as $key3 => $value3) {
                array_push($data_kata, $value3);

            }
            array_push($data_kata2, $data_kata);
            array_push($data_posisi, $key2);
        }

            array_push($data_kata3, $data_kata2);
            array_push($data_posisi_kalimat, $data_posisi);
            array_push($data_total_kalimat, $total_kalimat);
            array_push($data_kalimat, $kalimat);
    }
}

我想在另一个函数中访问变量$ data_3,$ data_posisi_kalimat,$ data_total_kalimat,$ data_kalimat。

示例我的功能:

function access_my_var(){
// I want to access all variable in above;
}

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

  1. 使用PHP explode函数将字符串分解为句子。
  2. 在第二个foreach循环中,使用PHP str_split
  3. 拆分句子
  4. 将所有数组合并为单个变量

    public function extract_fitur($string, $judul)
    {
        $data_total_kalimat=array();
        $data_kalimat=array();
        $data_posisi_kalimat=array();
        $data_kata3=array();
    
        $array_string = explode(". ", $string);
        foreach ($array_string as $key => $value) {
            $kalimat=$value;
            $total_kalimat=count($value);
            $data_posisi=array();
            $data_kata2=array();
    
            foreach (str_split($value) as $key2 =>$value2) {
                $data_kata=array();
                $pecah_kata=explode(" ", $value2);  
                foreach ($pecah_kata as $key3 => $value3) {
                    array_push($data_kata, $value3);
    
                }
                array_push($data_kata2, $data_kata);
                array_push($data_posisi, $key2);
            }
    
            array_push($data_kata3, $data_kata2);
            array_push($data_posisi_kalimat, $data_posisi);
            array_push($data_total_kalimat, $total_kalimat);
            array_push($data_kalimat, $kalimat);
        }
    
        $return_variable = array();
        $return_variable['data_total_kalimat'] = $data_total_kalimat;
        $return_variable['data_kalimat'] = $data_kalimat;
        $return_variable['data_posisi_kalimat'] = $data_posisi_kalimat;
        return $return_variable;
    }