We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16551 ☆ A M B ☆
    • 54 Posts
    I have a snippet that creates an array.

    How can I use and work with this array in another snippet?

    Snippet "createArray"
    <?php
    $array = array();
    $array[] = 'test';
    return $array;


    Snippet that's calling "createArray"
    <?php
    $output = $modx->runSnippet('createArray');
    return $output[0];


    This returns "Array" on the page.

    MODX Revolution 2.2.0-pl2 (traditional)
      • 22303 MODX Staff
      • 10,725 Posts
      Snippets only return strings. You could set a placeholder with the array value in one snippet and get that placeholder in other snippets, or you could return a JSON string of the array data and consume it using $modx->fromJSON(). e.g.

      <?php
      $array=array('test');
      return $modx->toJSON($array);


      <?php
      $array = $modx->fromJSON($modx->runSnippet('createArray'));
      return print_r($array, true);
        • 16551 ☆ A M B ☆
        • 54 Posts
        Thank you!
          • 40605
          • 5 Posts
          What about Evo?
          I also need to be able getting array of data after calling snippet in other snippet!
          (((
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Whenever you get an array you have to loop through the array and deal with its elements. How you do that would depend on exactly what kind of array the internal snippet is returning, and exactly what you want to do with the data in the array.

            The PHP documentation is a little dry, so I tend to refer to this http://oreilly.com/catalog/progphp/chapter/ch05.html

            And if you're really into learning some interesting stuff, there's this http://technosophos.com/content/php-arrays-are-not-arrays
            [ed. note: sottwell last edited this post 14 years, 2 months ago.]
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 40605
              • 5 Posts
              Thanks for the answer, but nevertheless, I do not understand how little I get the right information.
              For an example: [[test1]] return $data[‘name’]=modx; and $data[‘ver’]=‘evo’; how read all data in [[test2]
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                It's hard to say without knowing just what the runSnippet is returning.

                You can use a simple foreach loop:

                foreach($myarray as $key->$value) {
                
                    $output .= $key . ' - ' . $value ;
                }
                
                return $output;
                
                
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 40605
                  • 5 Posts
                  i found easy solution…json_encode and json_decode and it works!)))
                  Thanks for help!
                  But may be you can answer why in any template (even blank)
                  there is such strange code:
                  <style id="_clearly_component__css" type="text/css»>
                  #next_pages_container {
                  width: 5px; 
                  hight: 5px; 
                  position: absolute; 
                  top: -100px; 
                  left: -100px; 
                  z-index: 2147483647 !important; 
                  } </style>
                  <div id=«_clearly_component__next_pages_container»></div>

                  just before </html>
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Looks like you have either a MODx snippet running, or some Javascript that inserts that to style its paging menu. Perhaps the YUI javascript library?
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 40605
                      • 5 Posts
                      and again found solution)) somebody told me at russian mode community!
                      It’s because of evernote clipper) only in safari))
                      Thanks thanks again for your time!)