http_build_query problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

http_build_query problem

Post by kc11 »

Hi everyone,

I am trying to iterate through an array of variables, and for each one tack additional information on the end:

Code: Select all


foreach ($VariableArray as $key => $value) {
                 
               $value= $value.http_build_query($extra);
               
               
             }

when I do this I get the following error:
Warning: http_build_query() [function.http-build-query]: Parameter 1 expected to be Array or Object.
Can anyone explain this?

Thanks in advance,

K C
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: http_build_query problem

Post by Weirdan »

http_build_query() expects array, like this:

Code: Select all

echo http_build_query(array(
   'login' => 'weirdan',
   'password' => 'secret',
)); // will echo something like 'login=weirdan&password=secret'
your 'extra' variable obviously does not hold an array.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: http_build_query problem

Post by Jonah Bron »

What is $extra? The purpose of http_build_query() is to take an associative array and turn it into a URL-encoded HTTP query string. See the examples in the manual here:

http://php.net/http-build-query
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: http_build_query problem

Post by kc11 »

Hi Guys ,

Thanks for looking at this. To give you more detail on what I am trying to do:

I have an associative array ($VariableArray ) with multiple keys=>values, lets say they 'a'=>'d' , 'b'=>'e', and 'c'=>'f'. I have a second array with the same keys, but with different values, ( lets say g, h, i ) that I would like to add on the end ( This is $extra)

The result of the first iteration would be :

'a' =>'d,g'
'b' =>'e,h'
'c' =>'f,i'

The next iteration would generate:

'a' =>'d,g,j'
'b' =>'e,h,k'
'c' =>'f,i,l'

So essentially what I am trying to create is a 2 dimensional array , represented as a string

After reading online about this, I got the idea that " http_build_query()" , would be the best way to go. Is there a better way to do this?

KC
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: http_build_query problem

Post by AbraCadaver »

I'm not entirely sure I understand, but something like:

Code: Select all

foreach ($VariableArray as $key => &$value) {                 
   if(isset($extra[$key])) {
      $value .= ',' . $extra[$key];
   }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: http_build_query problem

Post by kc11 »

Thanks, AbraCadaver.

It works. I was over complicating things by looking for a function to do this. :oops:

Best regards,

KC
Post Reply