나는 간단한 템플릿 엔진을 가지고 있으며 assoc 배열로부터 데이터를 받는데 문제가있다. 아무도 나에게 충고를 줄 수 있니? getStatisticData
메서드에서 assoc 배열을 첫 번째 변수 $data
으로 지정합니다. 내 입력 배열의 형식은 :문자열 변환 php, assoc 배열
[0] => Array
(
[OrderNumber] => 1
[Name] => Zahid
[Total revenue] => 8363.38
)
나는 그것이 foreach
를 사용하여 데이터를 얻으려고하지만 그것이 작동하지 않습니다. 나는 TemplateGen.php
에서 통지
Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 34
protected function getStatisticData($data, $template){
$text = "";
if($data === false) {
return "We don't have any data in database";
}
foreach($data as $key => $value){
$data[$key] = $value;
$text .= $this->template_gen->getReplaceTemplate($data ,$template);
}
return $text;
}
getReplaceContent
및 관련 방법을 받고 있어요 :
private function getReplaceContent($dataString, $content)
{
$search = array();
$replace = array();
$i = 0;
foreach ($dataString as $key => $value) {
$search[$i] = "%$key%";
$replace[$i] = $value;
$i++;
}
return str_replace($search, $replace, $content); ## LINE 34
}
function getReplaceTemplate($dataString, $template)
{
return $this->getReplaceContent($dataString, $this->getTemplate($template));
}
function getTemplate($name)
{
return $content = file_get_contents($this->config->tpl_path . $name . ".tpl");
}
UPDATE :
내가 가진 2 개의 새로운 오류
,691,363 (210)Warning: implode(): Invalid arguments passed in C:\Server\htdocs\Task\lib\TemplateGen.php on line 28 Fatal error: Cannot redeclare add_percent() (previously declared in C:\Server\htdocs\Task\lib\TemplateGen.php:25) in C:\Server\htdocs\Task\lib\TemplateGen.php on line 25
행 25 :
function add_percent($i) {
행 28 :
return implode("", str_replace(array_map("add_percent", array_keys($dataString)), array_values($dataString),$content));
UPDATE2 :
이론적으로, 새로운 메소드로 제공되는 모든 것이 잘 작동합니다. 그러나 I'am 대신 당신의 내 getStatisticData
를 사용하는 경우 시작
Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 44`
line 44 :
return str_replace(
array_map($addPercent, array_keys($data)), array_values($data), $template
);
에 있었다 그러나 같은 문제는 작동이 있지만 다른 많은 오류가 있습니다
내 방법 :
protected function getStatisticData($data, $template){
$text = array();
if($data === false) {
return "We don't have any data in database";
}
$i=0;
foreach($data as $dataString){
if (!empty($data[$i+1])){
foreach($dataString as $key => $value){
$dataString[$i][$key] = $dataString[$i][$value];
}}
$text .= $this->template_gen->getReplaceTemplate($dataString ,$template);
}
return $text;
}
왜 이렇게하고 있습니까? foreach ($ data = $ value) {$ data [$ key] = $ value;'}?! 위의 주석에서 언급 한 바와 같이 –
은'$ data [$ key] = $ value;'는'$ value = $ value;'또는'$ data [$ key] = $ data [$ key];' –
btw,'TemplateGen'의 34 번째 줄은 어디에 있습니까? 또한 getReplaceTemplate이 아닌'getReplaceContent'를 게시했습니다. –