title: Set array with the previous value if it's current value is zero - php
body: <p>I want to replace the value '0' in the array values with the previous non-zero number.</p>
  
  <p>The current structure of array is as below: </p>
  
  <p>Could anyone help?</p>
  
  <pre><code>Array
  (
      [2016-06-16] =&gt; -143
      [2016-06-17] =&gt; 0
      [2016-06-18] =&gt; 0
      [2016-06-19] =&gt; 0
      [2016-06-20] =&gt; -28
      [2016-06-21] =&gt; 517
      [2016-06-22] =&gt; 0
      [2016-06-23] =&gt; -218
      [2016-06-24] =&gt; 0
      [2016-06-25] =&gt; 27
      [2016-06-26] =&gt; 0
      [2016-06-27] =&gt; 0
      [2016-06-28] =&gt; 0
      [2016-06-29] =&gt; 0
      [2016-06-30] =&gt; -798
      [2016-07-01] =&gt; 0
      [2016-07-02] =&gt; 0
      [2016-07-03] =&gt; 0
      [2016-07-04] =&gt; 0
      [2016-07-05] =&gt; 0
      [2016-07-06] =&gt; 0
      [2016-07-07] =&gt; 0
  
  )
  </code></pre>
  
  <p>Expected Structure</p>
  
  <pre><code>Array
      (
          [2016-06-16] =&gt; -143
          [2016-06-17] =&gt; -143
          [2016-06-18] =&gt; -143
          [2016-06-19] =&gt; -143
          [2016-06-20] =&gt; -28
          [2016-06-21] =&gt; 517
          [2016-06-22] =&gt; 517
          [2016-06-23] =&gt; -218
          [2016-06-24] =&gt; -218
          [2016-06-25] =&gt; 27
          [2016-06-26] =&gt; 27
          [2016-06-27] =&gt; 27
          [2016-06-28] =&gt; 27
          [2016-06-29] =&gt; 27
          [2016-06-30] =&gt; -798
          [2016-07-01] =&gt; -798
          [2016-07-02] =&gt; -798
          [2016-07-03] =&gt; -798
          [2016-07-04] =&gt; -798
          [2016-07-05] =&gt; -798
          [2016-07-06] =&gt; -798
          [2016-07-07] =&gt; -798
  
      )
  </code></pre>
  
  <p>I tried using below code, but it shows error and on changing it changes the first value at the maximum.</p>
  
  <pre><code>        foreach ($jsonstatisticsData as $arrdata=&gt; $val)
          {
              if($val==0){
  
      $prevarray=prev($jsonstatisticsData);
      $prev_value= $prevarray[$arrdata];
      $jsonstatisticsData[$arrdata]=$prev_value;
  
              }
  
          }
  </code></pre>