title: Array to string conversion php, assoc array
body: <p>I have a simple Template engine and I have a problem giving it a data from assoc array. Can anybody give me an advice? In method <code>getStatisticData</code>, I give an assoc array as first variable <code>$data</code>. My input array is in the form:</p>
  
  <pre><code>[0] =&gt; Array
      (
          [OrderNumber] =&gt; 1
          [Name] =&gt; Zahid
          [Total revenue] =&gt; 8363.38
      )
  </code></pre>
  
  <p>I'm trying to get data from it using <code>foreach</code> but it doesn't work. I'm getting notice </p>
  
  <blockquote>
    <p>Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 34</p>
  </blockquote>
  
  <pre><code>protected function getStatisticData($data, $template){  
  
          $text = "";
          if($data === false) {
              return "We don't have any data in database";
          }
          foreach($data as $key =&gt; $value){
  
                  $data[$key] = $value;
                  $text .= $this-&gt;template_gen-&gt;getReplaceTemplate($data ,$template);
                  }
  
          return $text;
      }
  </code></pre>
  
  <p><code>getReplaceContent</code> and associated methods from <code>TemplateGen.php</code>:</p>
  
  <pre><code> private function getReplaceContent($dataString, $content)
      {
          $search = array();
          $replace = array();
          $i = 0;
          foreach ($dataString as $key =&gt; $value) {
              $search[$i] = "%$key%";
              $replace[$i] = $value;
              $i++;
          }
          return str_replace($search, $replace, $content);  ## LINE 34
      }
  
  function getReplaceTemplate($dataString, $template)
      {
          return $this-&gt;getReplaceContent($dataString, $this-&gt;getTemplate($template));
      }
  
  function getTemplate($name)
      {
          return $content = file_get_contents($this-&gt;config-&gt;tpl_path . $name . ".tpl");
      }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I got 2 new errors</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>Line 25:</p>
  
  <pre><code>function add_percent($i) {
  </code></pre>
  
  <p>Line 28:</p>
  
  <pre><code>return implode("", str_replace(array_map("add_percent", array_keys($dataString)), array_values($dataString),$content));
  </code></pre>
  
  <p>UPDATE2:<br>
  In theory, everything provided in new method should work very well. But there are same problems which were at the beginning </p>
  
  <blockquote>
    <p>Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 44`<br>
    line 44 : </p>
  </blockquote>
  
  <pre><code>return str_replace(
              array_map($addPercent, array_keys($data)), array_values($data), $template
          );
  </code></pre>
  
  <p>But if I'am using my <code>getStatisticData</code> instead of yours, it works but there are many other errors<br>
  My method :</p>
  
  <pre><code> 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 =&gt; $value){
                  $dataString[$i][$key] = $dataString[$i][$value];
              }}
              $text .= $this-&gt;template_gen-&gt;getReplaceTemplate($dataString ,$template);
          }
          return $text;
  
      }
  </code></pre>