Jump to main content Jump to doc navigation

xPDOObject::toArray()

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

Syntax

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.

Examples

Get the values of the object in array format:

$object->set('name','John Lo');
$object->set('email','[email protected]');
$a = $object->toArray();
print_r($a);
// prints "Array ( [name] => John Lo [email] => [email protected] )"

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

$object->set('name','Mark');
$object->set('version','1.0');
$a = $object->toArray('dev_');
print_r($a);
// 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.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • CrewMark
  • Chris Fickling
  • deJaya
  • Lefthandmedia
  • Murray Wood
  • eydolan
  • Following Sea
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

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

Learn more