What am i doing wrong ?
echo try_add("test","2562782"); echo try_add("test","25627826"); function try_add($ct,$tankid){ $file = 'testFile_'.$ct.'.txt'; $pizza = file_get_contents($file); preg_match_all("/Tankid:(?P.*?);/", $pizza, $pie); //print_r($pie); //outputs //Array //( // [0] => Array // ( // [0] => Tankid:2562782; // [1] => Tankid:25627826; // [2] => Tankid:2562782; // [3] => Tankid:25627826; // ) // // [this] => Array // ( // [0] => 2562782 // [1] => 25627826 // [2] => 2562782 // [3] => 25627826 // ) // // [1] => Array // ( // [0] => 2562782 // [1] => 25627826 // [2] => 2562782 // [3] => 25627826 // ) // //) foreach($pie[this] as $thi){ if($thi == $tankid){ $inthis = "yes"; } else{ $inthis = "no"; } } if($inthis == "no"){ $myFile = "testFile_$ct.txt"; $fh = fopen($myFile, 'a'); $stringData = "Tankid:$tankid;\n"; fwrite($fh, $stringData); fclose($fh); return "This '$tankid' has been added in this file '$myFile' \n"; } else{ return "This '$tankid' is already in this file '$myFile' \n"; } } //Code outputs //Notice: Use of undefine开发者_开发百科d constant this - assumed 'this' in \xampp\htdocs\new.php on line 39 //This '2562782' has been added in this file 'testFile_test.txt' //Notice: Use of undefined constant this - assumed 'this' in \xampp\htdocs\new.php on line 39 //This '25627826' has been added in this file 'testFile_test.txt'
Change
foreach($pie[this] as $thi){
Into
foreach($pie['this'] as $thi){
You just need to rewrite
foreach($pie[this] as $thi){
into
foreach($pie[$this] as $thi){
You could even use
foreach($pie as $thi){
精彩评论