PHP - Función http_build_query ()

La función http_build_query () puede generar una cadena de consulta codificada en URL.

Sintaxis

string http_build_query( 
   mixed $query_data [, string $numeric_prefix [, string $arg_separator [, 
      int $enc_type = PHP_QUERY_RFC1738 ]]
   ] 
)

La función http_build_query () puede generar una cadena de consulta codificada en URL a partir de una matriz asociativa (o indexada) proporcionada.

La función http_build_query () puede devolver una cadena con codificación URL.

Ejemplo

<?php
   $data = array(
      "foo" => "bar",
      "baz" => "boom",
      "cow" => "milk",
      "php" => "hypertext processor"
   );

   echo http_build_query($data) . "\n";
   echo http_build_query($data, '', '&');
?>

Ejemplo

<?php
   $data = array("foo", "bar", "baz", "boom", "cow" => "milk", "php" => "hypertext processor");

   echo http_build_query($data) . "\n";
   echo http_build_query($data, 'myvar_');
?>

Ejemplo

<?php
   $data = array(
      "user" => array(
         "name" => "Bob Smith",
         "age"  => 47,
         "sex"  => "M",
         "dob"  => "5/12/1956"
      ),
      "pastimes" => array("golf", "opera", "poker", "rap"),
      "children" => array(
         "bobby" => array("age"=>12, "sex"=>"M"),
         "sally" => array("age"=>8, "sex"=>"F")
      ),
      "CEO"
   );
   echo http_build_query($data, "flags_");
?>

Ejemplo

<?php
   class parentClass {
      public $pub = "publicParent";
      protected $prot = "protectedParent";
      private $priv = "privateParent";
      public $pub_bar = Null;
      protected $prot_bar = Null;
      private $priv_bar = Null;

      public function __construct(){
         $this->pub_bar  = new childClass();
         $this->prot_bar = new childClass();
         $this->priv_bar = new childClass();
      }
   }
   class childClass {
      public $pub = "publicChild";
      protected $prot = "protectedChild";
      private $priv = "privateChild";
   }
   $parent = new parentClass();
   echo http_build_query($parent);
?>