<code><span>1.</span><span><span>function</span><span>toArray</span><span>(<span>$obj</span>)</span> {</span><span>if</span> (is_object(<span>$obj</span>)) <span>$obj</span> = (<span>array</span>)<span>$obj</span>; <span>if</span> (is_array(<span>$obj</span>)) { <span>$new</span> = <span>array</span>(); <span>foreach</span> (<span>$obj</span><span>as</span><span>$key</span> => <span>$val</span>) { <span>$new</span>[<span>$key</span>] = toArray(<span>$val</span>); } } <span>else</span> { <span>$new</span> = <span>$obj</span>; } <span>return</span><span>$new</span>; }</code>
<code><span>2.</span><span><span>function</span><span>objectToArray</span><span>(<span>$object</span>)</span> {</span><span>if</span>(!is_object(<span>$object</span>) && !is_array(<span>$object</span>)) <span>return</span><span>$object</span>; <span>return</span> array_map(<span>'objectToArray'</span>, (<span>array</span>) <span>$object</span>); }</code>
<code><span>3.</span>$<span><span>object</span> = <span>(object)</span> $<span>array</span>;</span></code>
<code><span>4.</span><span>$object</span> = <span>new</span> stdClass(); <span>foreach</span> (<span>$array</span><span>as</span><span>$key</span> => <span>$value</span>) { <span>$object</span>-><span>$key</span> = <span>$value</span>; }</code>
<code><span>5.</span><span>// Cast to an array</span><span>$array</span> = (<span>array</span>) <span>$object</span>;</code>
<code><span>5.</span><span>// get_object_vars</span><span>$array</span> = get_object_vars(<span>$object</span>);</code>
<code><span>6.</span><span>/** * 对象转数组 * array_map(__FUNCTION__, $array); 作用到数组中的每个值上,每个值都乘以本身,并返回带有新值的数组 * is_object($object) ? get_object_vars($object) : $object; 返回由对象属性组成的关联数组 *<span> @param</span> $object *<span> @return</span> array|void */</span><span>public</span><span>static</span><span><span>function</span><span>objectToArray</span><span>(<span>$object</span>)</span>{</span><span>$object</span> = (<span>array</span>)<span>$object</span>; <span>foreach</span>(<span>$object</span><span>as</span><span>$k</span>=><span>$v</span>){ <span>if</span>(gettype(<span>$v</span>) == <span>'resource'</span>){ <span>return</span>; } <span>if</span>(gettype(<span>$v</span>) == <span>'object'</span> || gettype(<span>$v</span>) == <span>'array'</span>){ <span>$object</span>[<span>$k</span>] = (<span>array</span>) StringAndArrayHelper::objectToArray(<span>$v</span>); } } <span>return</span><span>$object</span>; }</code>
<code><span>7.</span><span>/** * 数组转对象 *<span> @param</span> $array *<span> @return</span> object|void */</span><span>public</span><span>static</span><span><span>function</span><span>arrayToObject</span><span>(<span>$array</span>)</span>{</span><span>if</span>(gettype(<span>$array</span>) != <span>'array'</span>){ <span>return</span>; } <span>foreach</span>(<span>$array</span><span>as</span><span>$k</span>=><span>$v</span>){ <span>if</span>(gettype(<span>$v</span>) == <span>'array'</span> || getType(<span>$v</span>) == <span>'object'</span>){ <span>$array</span>[<span>$k</span>] = (object) StringAndArrayHelper::arrayToObject(<span>$v</span>); } } <span>return</span> (object)<span>$array</span>; }</code>
以上、内容の側面も含めて PHP のオブジェクトと配列の変換について紹介しましたが、PHP チュートリアルに興味のある友人に役立つことを願っています。