Page 1 of 1
http_build_query problem
Posted: Mon Jul 04, 2011 9:52 pm
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
Re: http_build_query problem
Posted: Mon Jul 04, 2011 9:55 pm
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.
Re: http_build_query problem
Posted: Mon Jul 04, 2011 9:57 pm
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
Re: http_build_query problem
Posted: Tue Jul 05, 2011 8:52 am
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
Re: http_build_query problem
Posted: Tue Jul 05, 2011 10:28 am
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];
}
}
Re: http_build_query problem
Posted: Wed Jul 06, 2011 9:41 am
by kc11
Thanks, AbraCadaver.
It works. I was over complicating things by looking for a function to do this.
Best regards,
KC