API15

JArrayHelper/fromObject: Difference between revisions

From Joomla! Documentation

m clean up
m preparing for archive only
 
Line 74: Line 74:


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=fromObject
  category=fromObject
  category=JArrayHelper
  category=JArrayHelper
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*

Latest revision as of 00:18, 25 March 2017

Description

Utility function to map an object to an array


<! removed transcluded page call, red link never existed >

Syntax

fromObject($p_obj, $recurse=true, $regex=null)
Parameter Name Default Value Description
$p_obj The source object
$recurse true True to recurve through multi-level objects
$regex null An optional regular expression to match on field names

Returns

array The array mapped from the given object

Defined in

libraries/joomla/utilities/arrayhelper.php

Importing

jimport( 'joomla.utilities.arrayhelper' );

Source Body

function fromObject( $p_obj, $recurse = true, $regex = null )
{
        $result = null;
        if (is_object( $p_obj ))
        {
                $result = array();
                foreach (get_object_vars($p_obj) as $k => $v)
                {
                        if ($regex)
                        {
                                if (!preg_match( $regex, $k ))
                                {
                                        continue;
                                }
                        }
                        if (is_object( $v ))
                        {
                                if ($recurse)
                                {
                                        $result[$k] = JArrayHelper::fromObject( $v, $recurse, $regex );
                                }
                        }
                        else
                        {
                                $result[$k] = $v;
                        }
                }
        }
        return $result;
}


<! removed transcluded page call, red link never existed >

Examples

Code Examples