本方法主要是应用于迭代对象。我应用的地方是simplexml中的simplexml_load_string()上,因为返回的全是对象,如果提取数据比较麻烦,所以应用了下面的函数。

返回的是一个层次比较分明的数组对象,希望对大家有所帮助,来源WEB开发笔记(www.chhua.com)。

1
2
3
4
5
6
7
8
9
10
<span style="color: #000000;font-weight: bold">function</span> object_to_array<span style="color: #009900">(</span><span style="color: #000088">$obj</span><span style="color: #009900">)</span>
<span style="color: #009900">{</span>
 <span style="color: #000088">$_arr</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/is_object" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_object</span></a><span style="color: #009900">(</span><span style="color: #000088">$obj</span><span style="color: #009900">)</span> ? <a rel="noopener noreferrer nofollow" href="http://www.php.net/get_object_vars" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">get_object_vars</span></a><span style="color: #009900">(</span><span style="color: #000088">$obj</span><span style="color: #009900">)</span> <span style="color: #339933">:</span> <span style="color: #000088">$obj</span><span style="color: #339933">;</span>
 <span style="color: #b1b100">foreach</span> <span style="color: #009900">(</span><span style="color: #000088">$_arr</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$key</span> <span style="color: #339933">=></span> <span style="color: #000088">$val</span><span style="color: #009900">)</span>
 <span style="color: #009900">{</span>
  <span style="color: #000088">$val</span> <span style="color: #339933">=</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$val</span><span style="color: #009900">)</span> <span style="color: #339933">||</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/is_object" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_object</span></a><span style="color: #009900">(</span><span style="color: #000088">$val</span><span style="color: #009900">)</span><span style="color: #009900">)</span> ? object_to_array<span style="color: #009900">(</span><span style="color: #000088">$val</span><span style="color: #009900">)</span> <span style="color: #339933">:</span> <span style="color: #000088">$val</span><span style="color: #339933">;</span> 
  <span style="color: #000088">$arr</span><span style="color: #009900">[</span><span style="color: #000088">$key</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #000088">$val</span><span style="color: #339933">;</span> 
 <span style="color: #009900">}</span>
 <span style="color: #b1b100">return</span> <span style="color: #000088">$arr</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>