Home > Backend Development > PHP Tutorial > PHP object and array conversion

PHP object and array conversion

WBOY
Release: 2016-07-28 08:26:15
Original
930 people have browsed it

<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>
Copy after login
<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>
Copy after login
<code><span>3.</span>$<span><span>object</span> = <span>(object)</span> $<span>array</span>;</span></code>
Copy after login
<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>
Copy after login
<code><span>5.</span><span>// Cast to an array</span><span>$array</span> = (<span>array</span>) <span>$object</span>;</code>
Copy after login
<code><span>5.</span><span>// get_object_vars</span><span>$array</span> = get_object_vars(<span>$object</span>);</code>
Copy after login
<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>
Copy after login
<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>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced PHP object and array conversion, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template