Jump to main content Jump to doc navigation


Copies the object fields and corresponding values to an associative array.


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

array toArray(
   [string $keyPrefix = ''],
   [boolean $rawValues = false],
   [boolean $excludeLazy = false],
   [boolean|integer|string|array $inludeRelated = false])
  • keyPrefix: an optional prefix to prepend to each fields' keys.
  • rawValues: optional flag to get the raw value (true) or to use xPDOObject->get(). Typically will want to use ->get().
  • excludeLazy: An option flag indicating if you want to exclude lazy fields from the resulting array; the default behavior is to include them which means the object will query the database for the lazy fields before providing the value.
  • includeRelated: Describes if and how to include loaded related object fields.
    • As an integer all loaded related objects in the graph up to that level of depth will be included.
    • As a string, only loaded related objects matching the JSON graph representation will be included.
    • As an array, only loaded related objects matching the graph array will be included.
    • As boolean true, all currently loaded related objects will be included.


Get the values of the object in array format:

$object->set('name','John Lo');
$a = $object->toArray();
// prints "Array ( [name] => John Lo [email] => jlo@gmail.com )"

Get the values of the object, but with their keys prefixed with 'dev_'

$a = $object->toArray('dev_');
// prints "Array ( [dev_name] => Mark [dev_version] => 1.0 )"

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.


  • modmore
  • Jens Wittmann – Gestaltung & Entwicklung
  • Digital Penguin
  • eydolan
  • deJaya
  • Following Sea
  • Sepia River Studios
  • Nick Clark
  • Lefthandmedia
  • YJ
  • Murray Wood
  • Dannevang Digital
  • Richard


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

Learn more