We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20196
    • 13 Posts
    I'm trying to output a JSON array from a snippet call on a resource, that is setup as json/inline

    $arr = $modx->getCollection('className');
    return $modx->toJSON($arr);
    

    But i get this weird response repeated;
    <b>Warning</b>: json_encode() [<a href='function.json-encode'>function.json-encode</a>]: recursion detected in <b>/home/site/core/xpdo/xpdo.class.php</b> on line <b>2327</b>




    If I run through the array in a foreach loop like this;
    foreach($arr as $key)
    {
    echo $key->get('value');
    }
    


    Every value gets outputed.

    What am I missing.

    //Moonkin
      • 4172
      • 5,888 Posts
      you will need first need to create arrays of your object-collection

      $collection = $modx->getCollection('className');
      foreach($collection as $object)
      {
          $arr[] = $object->toArray(); 
      }
      
      return $modx->toJSON($arr);
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 20196
        • 13 Posts
        Thanks Bruno, that did it.