We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17667
    • 108 Posts
    I have a snippet which serves to hold a PHP array that gets passed to other snippets. Lets call it "baseArray":
    $category_array = array(
    	"acupuncture" => "Acupuncture",
    	"advertising" => "Advertising",
    	"apparel" => "Apparel",
    );
    
    return $category_array;


    I would like to enable another snippet to take the array and process it as a PHP array. But when I try something like this it fails:
    $category_array = $modx->runSnippet('baseArray');
    foreach($category_array as $category_id => $category_name) {
    	$output .= '<li>' . $category_name . '</li>';
    }
    return $output;


    The above snippet does not return anything. It is receiving some sort of object from the first snippet but it does not appear to be a straight PHP array.

    Any help greatly appreciated.
      • 22303 MODX Staff
      • 10,725 Posts
      Snippets return strings in MODX, never anything else. If you want to use other types of data like this, you can set the variable (array, object, whatever) as a placeholder in one Snippet and access it by key from another that is processed after the placeholder is set.
        • 17667
        • 108 Posts
        Thanks! That makes sense. I haven't used placeholders in snippets before so I will read up on that.

        In the meantime I was able to work it using JSON encoding. So the first snippet becomes:
        $category_array = array(
            "acupuncture" => "Acupuncture",
            "advertising" => "Advertising",
            "apparel" => "Apparel",
        );
         
        return json_encode($category_array);


        And the second snippet becomes:
        $category_array = $modx->runSnippet('baseArray');
        $category_array = json_decode($category_array,true);
        foreach($category_array as $category_id => $category_name) {
            $output .= '<li>' . $category_name . '</li>';
        }
        return $output;


        Works great!
          • 4172
          • 5,888 Posts
          you can also return a Json-string:

          $category_array = array(
              "acupuncture" => "Acupuncture",
              "advertising" => "Advertising",
              "apparel" => "Apparel",
          );
           
          return $modx->toJson($category_array);
          



          $category_array = $modx->runSnippet('baseArray');
          $category_array = $modx->fromJson($category_array);
          foreach($category_array as $category_id => $category_name) {
              $output .= '<li>' . $category_name . '</li>';
          }
          return $output;
          

            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 17667
            • 108 Posts
            Looks like we both had the same idea Bruno17 but I didn't know about the MODX Json syntax. Much cleaner!