XeroPHP\Helpers::flattenAssocArray PHP Метод

flattenAssocArray() публичный статический Метод

Generic function to flatten an associative array into an arbitrarily delimited string.
public static flattenAssocArray ( array $array, string $format, string | null $glue = null, boolean $escape = false ) : string | array
$array array
$format string
$glue string | null
$escape boolean
Результат string | array If no glue provided, it won't be imploded.
    public static function flattenAssocArray(array $array, $format, $glue = null, $escape = false)
    {
        $pairs = [];
        foreach ($array as $key => $val) {
            if ($escape) {
                $key = self::escape($key);
                $val = self::escape($val);
            }
            $pairs[] = sprintf($format, $key, $val);
        }
        //Return array if no glue provided
        if ($glue === null) {
            return $pairs;
        } else {
            return implode($glue, $pairs);
        }
    }

Usage Example

Пример #1
0
 public function send()
 {
     //Sign the request - this just sets the Authorization header
     $this->app->getOAuthClient()->sign($this);
     // configure curl
     $ch = curl_init();
     curl_setopt_array($ch, $this->app->getConfig('curl'));
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
     if (isset($this->body)) {
         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
     }
     //build header array.  Don't provide glue so it'll return the array itself.
     //Maybe could be a but cleaner but nice to reuse code.
     $header_array = Helpers::flattenAssocArray($this->getHeaders(), '%s: %s');
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
     $full_uri = $this->getUrl()->getFullURL();
     //build parameter array - the only time there's a post body is with the XML,
     //only escape at this point
     $query_string = Helpers::flattenAssocArray($this->getParameters(), '%s=%s', '&', true);
     if (strlen($query_string) > 0) {
         $full_uri .= "?{$query_string}";
     }
     curl_setopt($ch, CURLOPT_URL, $full_uri);
     if ($this->method === self::METHOD_POST || $this->method === self::METHOD_PUT) {
         curl_setopt($ch, CURLOPT_POST, true);
     }
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     if ($response === false) {
         throw new Exception('Curl error: ' . curl_error($ch));
     }
     $this->response = new Response($this, $response, $info);
     $this->response->parse();
     return $this->response;
 }
All Usage Examples Of XeroPHP\Helpers::flattenAssocArray