我的代码是这样的:
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;
}
答案 0 :(得分:1)
您需要执行以下操作:
explode
函数将字符串分解为句子。foreach
循环中,使用PHP str_split
将所有数组合并为单个变量
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;
}