有没有办法对数组进行子字符串化?
is there a way to substring an array?
我想对查询给出的数组进行子字符串化
控制器(EX.值(Year_Report_2019,Year_Report_2020)
$store=array();
$tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");
foreach($tables as $this){
$get= substr($this,-4);
$store[] = $get;
}
BLADE 想要 (2019,2020)
的显示结果
{{implode(",", $store)}}
错误
substr() expects parameter 1 to be string, object given
是的,implode
函数会为您完成:
{{implode(",", $store)}}
PHP 文档 here.
$array = ['Year_Report_2019','Year_Report_2020'];
array_walk($array, function(&$item) {
return $item = preg_replace("/\D/", '', $item);
});
首先,您可以按照自己的方式构建数组,只保留整数值。此操作可能发生在您的控制器中,因此您的视图中没有数组处理。
然后在您的视图中,您可以 return 并将新数组显示为逗号分隔的字符串,使用:
{{ implode(",",$array) }}
这里不需要使用 foreach 只需用你想要的表达式 implode 这个数组
{{implode(",", $store)}}
您对结果的解析有误。 (避免使用变量名 $this
)
$store=array();
$tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");
foreach($tables as $table){
$store[] = substr($table->Tables_in_database,-4);
}
我想对查询给出的数组进行子字符串化
控制器(EX.值(Year_Report_2019,Year_Report_2020)
$store=array();
$tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");
foreach($tables as $this){
$get= substr($this,-4);
$store[] = $get;
}
BLADE 想要 (2019,2020)
的显示结果{{implode(",", $store)}}
错误
substr() expects parameter 1 to be string, object given
是的,implode
函数会为您完成:
{{implode(",", $store)}}
PHP 文档 here.
$array = ['Year_Report_2019','Year_Report_2020'];
array_walk($array, function(&$item) {
return $item = preg_replace("/\D/", '', $item);
});
首先,您可以按照自己的方式构建数组,只保留整数值。此操作可能发生在您的控制器中,因此您的视图中没有数组处理。
然后在您的视图中,您可以 return 并将新数组显示为逗号分隔的字符串,使用:
{{ implode(",",$array) }}
这里不需要使用 foreach 只需用你想要的表达式 implode 这个数组
{{implode(",", $store)}}
您对结果的解析有误。 (避免使用变量名 $this
)
$store=array();
$tables = \DB::select("SHOW TABLES WHERE Tables_in_database LIKE '%Year_Report_%'");
foreach($tables as $table){
$store[] = substr($table->Tables_in_database,-4);
}