Maison >développement back-end >tutoriel php >PHP对象与数组转换

PHP对象与数组转换

WBOY
WBOYoriginal
2016-07-28 08:26:15924parcourir

<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>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了 PHP对象与数组转换,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:laravel51 安装debug插件Article suivant:php中global的使用