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

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • modmore
  • Jens Wittmann – Gestaltung & Entwicklung
  • Raffy
  • eydolan
  • Digital Penguin
  • Fabian Christen
  • Dannevang Digital
  • deJaya
  • Following Sea
  • Sepia River Studios
  • Anton Tarasov
  • Lefthandmedia
  • Nick Clark
  • Chris Fickling
  • Murray Wood
  • YJ
  • CrewMark
  • Richard

Budget

$305 per month—let's make that $500!

Learn more