IT Community - Software Programming, Web Development and Technical Support

Build GET/POST query based on associated array

This is a discussion on Build GET/POST query based on associated array within the PHP Programming forums, part of the Web Development category; Hi, http_build_query - This function support PHP 5 or above versions This function is used to Generates a URL-encoded query ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 11-05-2007, 11:33 PM
Gopisoft Gopisoft is offline
D-Web Sr.Programmer
 
Join Date: Feb 2007
Posts: 117
Gopisoft is on a distinguished road
Default Build GET/POST query based on associated array

Hi,

http_build_query - This function support PHP 5 or above versions

This function is used to Generates a URL-encoded query string from the associative (or indexed) array provided.

Code:
Syntax
string http_build_query ( array $formdata [, string $numeric_prefix 
[, string $arg_separator]] )

ChangeLog
arg_separator is added in PHP version 5.1.2
5.1.3 Square brackets are escaped.

Example: Simple usage of http_build_query()
PHP Code:
<?php
$data 
= array('foo'=>'bar',
              
'baz'=>'boom',
              
'cow'=>'milk',
              
'php'=>'hypertext processor');

echo 
http_build_query($data);
// foo=bar&baz=boom&cow=milk&php=hypertext+processor
echo http_build_query($data'''&amp;'); 
// foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor ]
?>
Example: http_build_query() with numerically index elements.
PHP Code:
<?php
$data 
= array('foo''bar''baz''boom''cow' => 'milk''php' =>'hypertext processor');
echo 
http_build_query($data) . "\n";
echo 
http_build_query($data'myvar_');
?>
Out Put
Code:
0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor
myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor
Example http_build_query() with complex arrays
PHP Code:
<?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_');
?>
output: (word wrapped for readability)
HTML Code:
user%5Bname%5D=Bob+Smith&user%5Bage%5D=47&user%5Bsex%5D=M&
user%5Bdob%5D=5%2F12%2F1956&pastimes%5B0%5D=golf&pastimes%5B1%5D=opera&
pastimes%5B2%5D=poker&pastimes%5B3%5D=rap&children%5Bbobby%5D%5Bage%5D=12&
children%5Bbobby%5D%5Bsex%5D=M&children%5Bsally%5D%5Bage%5D=8&
children%5Bsally%5D%5Bsex%5D=F&flags_0=CEO
Note: Only the numerically indexed element in the base array "CEO" received a prefix. The other numeric indices, found under pastimes, do not require a string prefix to be legal variable names

Example: Using http_build_query() with an object
PHP Code:
<?php
class myClass {
    var 
$foo;
    var 
$baz;

    function 
myClass() {
        
$this->foo 'bar';
        
$this->baz 'boom';
    }
}
$data = new myClass();
echo 
http_build_query($data); // foo=bar&baz=boom
?>
Thanks,
R.Gopi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-06-2007, 07:29 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Build GET/POST query based on associated array

Hi,
This is very useful function. But it works for sending all the array values to the querystring.

This function is very new for all including me.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Product-based Company and Projects-based Company : vigneshgets Software Testing 0 01-15-2008 11:03 PM
Build vigneshgets Software Testing 1 01-15-2008 10:00 PM
How to prepare Bug report for every day build? KiruthikaSambandam Software Testing 2 08-28-2007 06:57 AM
What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? Archer C# Programming 2 08-25-2007 03:00 AM
How to Post and get the Multidimension Array? varghese PHP Programming 1 07-11-2007 06:06 AM


All times are GMT -7. The time now is 06:52 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0