在字符串中加入php的foreach循环,怎么写?
发布网友
发布时间:2022-04-23 18:11
我来回答
共3个回答
热心网友
时间:2023-10-12 10:04
$string_process = " '明治'=> array( 'dbms'=>'mysql', 'username'=>'sa', 'password'=>'sa', 'hostpost'=>'3306', 'database'=>'meiji', 'projectname'=>'meiji', ),
'kona'=> array( 'dbms'=>'mysql', 'username'=>'sa', 'password'=>'sa', 'hostpost'=>'3306', 'database'=>'22', 'projectname'=>'kona', ),\n\n";
$new = array();
preg_match_all ("/(\'.+\)),/", $string_process, $matches);
// print_r($matches[1]);
//处理结果
// Array
// (
// [0] => '明治'=> array( 'dbms'=>'mysql', 'username'=>'sa', 'password'=>'sa', 'hostpost'=>'3306', 'database'=>'meiji', 'projectname'=>'meiji', )
// [1] => 'kona'=> array( 'dbms'=>'mysql', 'username'=>'sa', 'password'=>'sa', 'hostpost'=>'3306', 'database'=>'22', 'projectname'=>'kona', )
// )
热心网友
时间:2023-10-12 10:05
$string_process = preg_replace(array("/, *\\n/","/\', *\)/"),array("","')"),$string_process);
eval("\$arr = array(".$string_process.");");
//print_r($arr);
$str = "";
foreach($arr as $k=>$v){
$str.=$k.":";
foreach($v as $d){
$str.=$d.",";
}
}
trim($str,",");
echo $str;
是这个意思么
追问给个效果图!
追答$string_process = preg_replace(array("/, *\\n/","/\', *\)/"),array("","')"),$string_process);
eval("\$arr = array(".$string_process.");");
print_r($arr);
$str = "";
foreach($arr as $k=>$v){
$str.=$k.":";
foreach($v as $d){
$str.=$d.",";
}
}
$str = trim($str,",");
echo $str;
你直接放环境里运行一下不就好了
热心网友
时间:2023-10-12 10:05
foreach ($string_process as $value) {
echo $value;//这里你可以对$value执行任何操作
}
上面的表达式实质是依次取出$string_process中的值,并赋给$value追问额!你好像弄反了,我是要在$string_process中foreach循环出数据库的值,