将对象作为变量中的字符串传递

时间:2014-02-18 16:48:07

标签: php json

我有这部分代码:

public function setAction($action, $params = array()) {
    $actionUri = strtolower($action);
    $zanoxAuth = new ApiAuthorization();
    $zanoxAuth->setConnectId(__ZANOX_CONNECT_ID);
    $zanoxAuth->setSecretKey(__ZANOX_SECRET_KEY);
    /*
     * Default number of items in the JSON
     * response
     */
    $defaultResults = __ZANOX_JSON_DEFAULT_RESULTS;

    $zanoxAuth->setTimestamp(gmdate("D, d M Y H:i:s T"));
    $timestamp = $zanoxAuth->getTimestamp();
    $nonce = $zanoxAuth->getNonce();
    /*
     * Uri queries concatenation
     */
    $queries = '';
    if (is_array($params) && count($params) > 0)
        foreach ($params as $param => $value)
            $queries .= "&$param=$value";

    /*
     * Getting the first page of the JSON
     * response.
     */
    $results = $this->curlMe(__ZANOX_ENDPOINT . "$actionUri", "?connectid=" . __ZANOX_CONNECT_ID . "$queries&page=0&items=$defaultResults&date=$timestamp&nonce=$nonce&signature=" . $zanoxAuth->getSignature('GET/', $action, $nonce));
    $results = json_decode($results);
    /*
     * Checking if there are more items to get
     * by looping the pages.
     */
    if ($results->total > $defaultResults) {
        $pages = ceil($results->total / $defaultResults);

        for ($i = 1; $i < $pages; $i++) {

            $nonce = $zanoxAuth->getNonce();
            $addJSON = $this->curlMe(__ZANOX_ENDPOINT . "$actionUri", "?connectid=" . __ZANOX_CONNECT_ID . "$queries&page=$i&items=$defaultResults&date=$timestamp&nonce=$nonce&signature=" . $zanoxAuth->getSignature('GET/', $action, $nonce));
            $addJSON = json_decode($addJSON);
            /*
             * Adding JSON items to the main one.
             */
            foreach ((array) $addJSON->programApplicationItems->programApplicationItem as $newItem)
                $results->programApplicationItems->programApplicationItem[] = $newItem;
        }
    }

    $this->action = $results;
    return $this;
}

你看到$results->programApplicationItems->programApplicationItem[] = $newItem;我希望将对象的参数作为变量传递,例如:

$results->$action . "Items"->$action . "Item"[] = $newItem

显然以上都行不通。 我想过var_dumpvar_export,但我不确定它们会起作用。 有帮助吗? 非常感谢你。

1 个答案:

答案 0 :(得分:1)

使用花括号:{ }

$results->{$action . "Items"}->{$action . "Item"}[] = $newItem;