Welcome Guest, Not a member yet? Register   Sign In
Query a associative array from Database CI4
#2

You can place these in a helper and then use to convert.

PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('objectToArray'))
{
    /**
    * objectToArray ()
    * -------------------------------------------------------------------
    *
    * @param  $data
    * @return array
    */
    function objectToArray(object $data) : array
    {
        if (is_object($data))
        {
            /**
            * Gets the properties of the given object
            * with get_object_vars function
            */
            $data get_object_vars($data);
        }

        /**
        * Return array converted to object Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return (is_array($data)) ? array_map(__FUNCTION__$data) : $data;
    }
}

// -----------------------------------------------------------------------

if ( ! function_exists('arrayToObject'))
{
    /**
    * arrayToObject ()
    * -------------------------------------------------------------------
    *
    * @param  $data
    * @return object
    */
    function arrayToObject(array $data) : object
    
{
        /**
        * Return array converted to object Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
RE: Query a associative array from Database CI4 - by InsiteFX - 02-11-2022, 02:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB