Jump to main content Jump to doc navigation

xPDOObject::fromArray()

Sets object fields from an associative array of key => value pairs.

Syntax

API Docs: http://api.modxcms.com/xpdo/om/xPDOObject.html#fromArray

void fromArray(
   array $fldarray,
   [string $keyPrefix = ''],
   [boolean $setPrimaryKeys = false],
   [boolean $rawValues = false],
   [boolean $adhocValues = false]
)

Examples

Input the name of a person from an array.

$object->fromArray(array(
    'fname' => 'Boo',
    'lname' => 'Radley',
));
echo $object->get('fname').' '.$object->get('lname');
// prints "Boo Radley"

Strip 'ghost_' prefixes from the array provided:

$object->fromArray(array(
    'ghost_fname' => 'Nearly Headless',
    'ghost_lname' => 'Nick',
),'ghost_');
echo $object->get('fname').' '.$object->get('lname');
// prints "Nearly Headless Nick"

Creating a MODX resource:

$page = $modx->newObject('modResource');

$data = array(
    'pagetitle' => 'My Page',
    'description' => 'Why not?',
    // ... etc...
);

$page->fromArray($data);
$page->save();

See Also