Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回的数组产生意外结果_Php_Arrays_Modx_Modx Revolution - Fatal编程技术网

Php 返回的数组产生意外结果

Php 返回的数组产生意外结果,php,arrays,modx,modx-revolution,Php,Arrays,Modx,Modx Revolution,我有一个modx revolution代码段[实际上是ajax处理器],它将一些数据传递给另一个调用函数的代码段 <?php // processor.formdata $status = 'success'; $msg = ''; $output = $modx->runSnippet($_POST['snippet'],array( 'function' => $_POST['function'], 'formdata' => $_POST ));

我有一个modx revolution代码段[实际上是ajax处理器],它将一些数据传递给另一个调用函数的代码段

<?php
// processor.formdata

$status = 'success';

$msg = '';

$output = $modx->runSnippet($_POST['snippet'],array(
   'function' => $_POST['function'],
   'formdata' => $_POST
));

$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData status. '.$output['status']);
$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData message. '.$output['msg']);

$response_array = array(
    'status' => $output['status'],
    'msg' => $output['msg'],
    );

header('Content-type: application/json');

$output = json_encode($response_array);

return $output;
一切都工作并成功运行,但是$output=$modx runSnippet返回的值在每个数组键中都返回“A”

查看AmericaSurcharge.class.php中的日志,我发现:

(ERROR @ /index.php) Finished running updateSurchargeData. success
(ERROR @ /index.php) Finished running updateSurchargeData. this is the message to return.
紧接着processor.formdata中的:

[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData status. A
[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData message. A
因此,在从AmericaSurcharge.class.php返回$ouput数组和将其返回到processor.formdata之间,值发生了一些变化

我做错了什么

我做错了什么

您正在使用代码段:)

调用
$modx->runSnippet($snippetName)
时,它只返回字符串。不是数组或smth其他

请看modScript过程方法:

public function process($properties= null, $content= null) {
    parent :: process($properties, $content);
    if (!$this->_processed) {
        $scriptName= $this->getScriptName();
        $this->_result= function_exists($scriptName);
        if (!$this->_result) {
            $this->_result= $this->loadScript();
        }
        if ($this->_result) {
            if (empty($this->xpdo->event)) $this->xpdo->event = new stdClass();
            $this->xpdo->event->params= $this->_properties; /* store params inside event object */
            ob_start();
            $this->_output= $scriptName($this->_properties);
            $this->_output= ob_get_contents() . $this->_output;
            ob_end_clean();
            if ($this->_output && is_string($this->_output)) {
                /* collect element tags in the evaluated content and process them */
                $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
                $this->xpdo->parser->processElementTags(
                    $this->_tag,
                    $this->_output,
                    $this->xpdo->parser->isProcessingUncacheable(),
                    $this->xpdo->parser->isRemovingUnprocessed(),
                    '[[',
                    ']]',
                    array(),
                    $maxIterations
                );
            }
            $this->filterOutput();
            unset ($this->xpdo->event->params);
            $this->cache();
        }
    }
    $this->_processed= true;
    $this->xpdo->parser->setProcessingElement(false);
    /* finally, return the processed element content */
    return $this->_output;
}
但您仍然可以将代码段作为函数调用。大概是这样的:

if($modx->getParser()){
   $snippet= $modx->parser->getElement('modSnippet', $snippetName);
   if ($snippet instanceof modSnippet) {

    $s = $snippet->getScriptName();
    if(!function_exists($s)){
        $snippet->loadScript();
    }
    print_r($s());
   }
}
附言: 解决问题的最佳方法是使用真正的基于类的处理器,而不是类似处理器的处理器。 优点:

  • 返回您想要的任何内容
  • 一个处理器可以扩展另一个处理器
  • 规范答复格式
    试着在更多的地方添加日志语句,看看它发生在哪里。我不知道runSnippet只能返回字符串!我知道在返回$output数组之前,我在americanoverlay.class中将它转换为json&现在我可以让它按原样工作了。不过,你的建议是一个更好的办法。谢谢
    public function process($properties= null, $content= null) {
        parent :: process($properties, $content);
        if (!$this->_processed) {
            $scriptName= $this->getScriptName();
            $this->_result= function_exists($scriptName);
            if (!$this->_result) {
                $this->_result= $this->loadScript();
            }
            if ($this->_result) {
                if (empty($this->xpdo->event)) $this->xpdo->event = new stdClass();
                $this->xpdo->event->params= $this->_properties; /* store params inside event object */
                ob_start();
                $this->_output= $scriptName($this->_properties);
                $this->_output= ob_get_contents() . $this->_output;
                ob_end_clean();
                if ($this->_output && is_string($this->_output)) {
                    /* collect element tags in the evaluated content and process them */
                    $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
                    $this->xpdo->parser->processElementTags(
                        $this->_tag,
                        $this->_output,
                        $this->xpdo->parser->isProcessingUncacheable(),
                        $this->xpdo->parser->isRemovingUnprocessed(),
                        '[[',
                        ']]',
                        array(),
                        $maxIterations
                    );
                }
                $this->filterOutput();
                unset ($this->xpdo->event->params);
                $this->cache();
            }
        }
        $this->_processed= true;
        $this->xpdo->parser->setProcessingElement(false);
        /* finally, return the processed element content */
        return $this->_output;
    }
    
    if($modx->getParser()){
       $snippet= $modx->parser->getElement('modSnippet', $snippetName);
       if ($snippet instanceof modSnippet) {
    
        $s = $snippet->getScriptName();
        if(!function_exists($s)){
            $snippet->loadScript();
        }
        print_r($s());
       }
    }