>백엔드 개발 >PHP 튜토리얼 >PHP 객체 및 배열 변환

PHP 객체 및 배열 변환

WBOY
WBOY원래의
2016-07-28 08:26:15930검색

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

위에서 내용의 측면을 포함하여 PHP 개체 및 배열 변환을 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.