Array to string conversion php, assoc array -
i have simple template engine , have problem giving info assoc array. can give me advice? in method getstatisticdata
, give assoc array first variable $data
. input array in form:
[0] => array ( [ordernumber] => 1 [name] => zahid [total revenue] => 8363.38 )
i'm trying info using foreach
doesn't work. i'm getting notice
notice: array string conversion in c:\server\htdocs\task\lib\templategen.php on line 34
protected function getstatisticdata($data, $template){ $text = ""; if($data === false) { homecoming "we don't have info in database"; } foreach($data $key => $value){ $data[$key] = $value; $text .= $this->template_gen->getreplacetemplate($data ,$template); } homecoming $text; }
getreplacecontent
, associated methods templategen.php
:
private function getreplacecontent($datastring, $content) { $search = array(); $replace = array(); $i = 0; foreach ($datastring $key => $value) { $search[$i] = "%$key%"; $replace[$i] = $value; $i++; } homecoming str_replace($search, $replace, $content); ## line 34 } function getreplacetemplate($datastring, $template) { homecoming $this->getreplacecontent($datastring, $this->gettemplate($template)); } function gettemplate($name) { homecoming $content = file_get_contents($this->config->tpl_path . $name . ".tpl"); }
update:
i got 2 new errors
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
line 25:
function add_percent($i) {
line 28:
return implode("", str_replace(array_map("add_percent", array_keys($datastring)), array_values($datastring),$content));
update2: in theory, provided in new method should work well. there same problems @ origin
notice: array 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 );
but if i'am using getstatisticdata
instead of yours, works there many other errors method :
protected function getstatisticdata($data, $template){ $text = array(); if($data === false) { homecoming "we don't have info in database"; } $i=0; foreach($data $datastring){ if (!empty($data[$i+1])){ foreach($datastring $key => $value){ $datastring[$i][$key] = $datastring[$i][$value]; }} $text .= $this->template_gen->getreplacetemplate($datastring ,$template); } homecoming $text; }
your code rather inefficient @ present, , it's leading isuues. let's @ first method posted:
protected function getstatisticdata($data, $template){ $text = ""; if($data === false) { homecoming "we don't have info in database"; } foreach($data $key => $value){ $data[$key] = $value; $text .= $this->template_gen->getreplacetemplate($data ,$template); } homecoming $text; }
the foreach
loop doing same thing each key/value pair (plus $data[$key] = $value
redundant getting each key , value in foreach
loop), eliminate loop, , replace this:
protected function getstatisticdata($data, $template){ if ($data === false) { homecoming "we don't have info in database"; } homecoming $this->template_gen->getreplacetemplate($data, $template); }
similarly getreplacecontent
- you're using array keys , array values search , replacement values. can utilize php's handy array_keys
, array_values
instead of building new arrays. grab array keys need surrounded %
, easy using array_map
- define function take in string , add together %
either end of it, , array_map
array keys:
private function getreplacecontent($data, $template) { $addpercent = function( $i ){ homecoming "%$i%"; }; homecoming str_replace( array_map( $addpercent, array_keys($data)), array_values($data), $template ); }
now, calling getstatisticdata
homecoming template text replaced info in it.
sample input:
$arr = array( 'ordernumber' => 1, 'name' => 'zahid', 'total revenue' => '8363.38' ); $template = '<p>name: %name%<br> total revenue: %total revenue%<br> order number: %ordernumber%</p>';
output of getstatisticdata
:
<p>name: zahid<br> total revenue: 8363.38<br> order number: 1</p>
edit: not clear op input getstatisticdata
is, looks supposed array of associative arrays. if so, code getstatisticdata
should altered follows:
protected function getstatisticdata($dataarray, $template){ if ($dataarray === false) { homecoming "we don't have info in database"; } $text = ""; foreach ($dataarray $aa) { $text .= $this->template_gen->getreplacetemplate($aa, $template); } homecoming $text; }
sample input:
$arr = [ [ 'ordernumber' => 1, 'name' => 'zahid', 'total revenue' => '8363.38' ], [ 'ordernumber' => 2, 'name' => 'paul', 'total revenue' => '123.45' ], [ 'ordernumber' => 3, 'name' => 'jane', 'total revenue' => '567.89' ], ];
output:
<p>name: zahid<br> total revenue: 8363.38<br> order number: 1</p> <p>name: paul<br> total revenue: 123.45<br> order number: 2</p> <p>name: jane<br> total revenue: 567.89<br> order number: 3</p>
php arrays templates associative-array
No comments:
Post a Comment