title: PHP convert multidimensional multi-depth array to string
body: <p>The following is an example of a multi-dimensional array that I'm working with:</p>
  
  <pre><code>array:4 [
    "discount" =&gt; array:2 [
      0 =&gt; 10.0
      1 =&gt; 100.0
    ]
    "updated_at" =&gt; array:2 [
      0 =&gt; "2015-02-25 11:35:36"
      1 =&gt; "2015-02-25 11:35:57"
    ]
    "creator" =&gt; array:1 [
      "url" =&gt; array:2 [
        0 =&gt; null
        1 =&gt; null
      ] 
    ]
    "lines" =&gt; array:2 [
      0 =&gt; array:4 [
        "created_at" =&gt; array:2 [
          0 =&gt; "2015-02-25 11:35:36"
          1 =&gt; "2015-02-25 11:35:57"
        ]
        "updated_at" =&gt; array:2 [
          0 =&gt; "2015-02-25 11:35:36"
          1 =&gt; "2015-02-25 11:35:57"
        ]
        "products" =&gt; array:1 [
          0 =&gt; array:1 [
             "pivot" =&gt; array:1 [
               "order_line_id" =&gt; array:2 [
                 0 =&gt; 25
                 1 =&gt; 27
          ]
        ]
      ]
    ]
    "sites" =&gt; array:1 [
      0 =&gt; array:1 [
        "pivot" =&gt; array:1 [
          "order_line_id" =&gt; array:2 [
            0 =&gt; 25
            1 =&gt; 27
          ]
        ]
      ]
    ]
  ]
  1 =&gt; array:4 [
    "discount" =&gt; array:2 [
      0 =&gt; 10.0
      1 =&gt; 100.0
    ]
    "created_at" =&gt; array:2 [
      0 =&gt; "2015-02-25 11:35:36"
      1 =&gt; "2015-02-25 11:35:57"
    ]
    "updated_at" =&gt; array:2 [
      0 =&gt; "2015-02-25 11:35:36"
      1 =&gt; "2015-02-25 11:35:57"
    ]
    "products" =&gt; array:2 [
      0 =&gt; array:1 [
        "pivot" =&gt; array:1 [
          "order_line_id" =&gt; array:2 [
            0 =&gt; 26
            1 =&gt; 28
          ]
        ]
      ]
      1 =&gt; array:1 [
        "pivot" =&gt; array:1 [
          "order_line_id" =&gt; array:2 [
            0 =&gt; 26
            1 =&gt; 28
          ]
        ]
          ]
        ]
      ]
    ]
  ]
  </code></pre>
  
  <p>As you can see, each key has two values, 0 and 1. </p>
  
  <p>What I want to do is convert the multi-dimensional array into a string, in the format:</p>
  
  <blockquote>
    <p>discount changed from 10.0 to 100.0.</p>
    
    <p>updated_at changed from 2015-02-25 11:35:36 to 2015-02-25 11:35:57</p>
    
    <p>...</p>
    
    <p>...</p>
    
    <p>lines created_at changed from xxx to yyy</p>
    
    <p>lines products pivot order_line_id changed from 25 to 27.</p>
  </blockquote>
  
  <p>The latter strings are based on the key names for each level of the array.</p>
  
  <p>I'm nearly there, but it's not an ideal solution as it doesn't handle the level of nesting I need:</p>
  
  <pre><code>convertDifferencesArrayToString($difference)
  
  
  public function convertDifferencesArrayToString($array)
  {
      // Nothing was changed, or only the creator timestamps were touched.
      if(!$array || (sizeof($array)==1 &amp;&amp; isset($array['creator']))){
          return 'Submitted the same data';
      }
  
      $string = 'Changed ';
  
      foreach($array as $key=&gt;$value)
      {
          // Calculate the difference for standard values
          if(!$this-&gt;hasNestedRelations($value))
          {
              $string .= $this-&gt;convertValuesToString($key, $value);
          }
          else
          {
              foreach($value as $rel_key=&gt;$rel_val)
              {
                  $string .= $this-&gt;convertValuesToString($rel_key, $rel_val, $key);
              }
          }
  
      }
      return rtrim($string, ', ');
  }
  
  private function hasNestedRelations($value)
  {
      if(array_key_exists(0, $value) &amp;&amp; array_key_exists(1, $value)) {
          return false;
      }
  
      return true;
  }
  
  private function convertValuesToString($key, $value, $parent_key = null)
  {
      // The 'creator' parent key is excluded, just in case the user is editing
      // their own user account, which they made themselves.
  
      if($parent_key) {
          if($key=='name'){
              return sprintf('%s%s from %s to %s, ',
                  $parent_key ? ucwords($parent_key) : '',
                  '',
                  (is_null($value[0]) ? '[empty]' : $value[0]),
                  (is_null($value[0]) ? '[empty]' : $value[1])
              );
          }
      } else {
          if($key!='updated_at' &amp;&amp; ($value[0]!==$value[1]))
              return sprintf('%s%s from %s to %s, ',
                  $parent_key ? ucwords(str_replace('_', ' ', $parent_key)) . ': ' : '',
                  ucwords(str_replace('_', ' ', $key)),
                  (is_null($value[0]) ? '[empty]' : $value[0]),
                  (is_null($value[0]) ? '[empty]' : $value[1])
              );
      }
  
  }
  </code></pre>