Combinando 2 matrices para que se muestren correctamente en mi URL
Frecuentes
Visto 49 veces
0
I have an array $cat1 including
cat1[0]=>16 and cat1[1]=>16.
I also have this array:
$url_vars = array('text'=>$event->properties['text'],'SearchResultPagerPage'=>$thenextpage);
I need to put these combined into this URL function:
$this->URL('SearchResult','',$url_vars);
So that the resulting URL needs to look like this:
/SearchResult.html?text=cat&SearchResultPagerPage=1&cat1[]=1&cat1[]=16
Currently, if I combine them, I get this as the resulting combined array:
Array
(
[text] => cat
[SearchResultPagerPage] => 1
[0] => 1
[1] => 16
)
and this as the resulting URL:
SearchResult.html?&text=cat&SearchResultPagerPage=1&1=16
How do I form this so that it says cat1[]=1&cat1[]=16 instead of 1=16?
Thanks very much for any help anyone might offer!!
1 Respuestas
0
If you MUST use the URL function $this->URL('SearchResult','',$url_vars);
then one idea is to use indices in the cat1 array. That is:
$url_vars["cat1[0]"] = 1;
$url_vars["cat1[1]"] = 16;
This will result in your query string having
...&cat1[0]=1&cat1[1]=16
with the []'s probably escaped, but perhaps your server script can properly accommodate these indices. It is worth a try. Otherwise you'll have to generate the URL outside of the URL function, because you can't have a php array with identical keys "cat1[]" but two separate values.
EDIT: One other thing to try in case your URL function is intelligent enough:
$url_vars["cat1"] = [1,16];
Respondido el 10 de Septiembre de 13 a las 01:09
"you can't have a php array with identical keys "cat1[]" but two separate values." - ehh yes you can. - Jack
@Jack Not unless it's a multidimensional array (stackoverflow.com/questions/5445283/…) or some other trick. But not directly in the way the OP desires. What did you have in mind? - Matt
Estoy asumiendo que $var1
tiene [16,16]
y entonces http_build_query()
would make it work as expected. Unfortunately the OP wasn't very explicit about its contents. - Jack
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php or haz tu propia pregunta.
Just a suggestion - stop using
array()
y use[]
preferiblemente.array()
is painful on the eyes. - m59Square bracket characters are not allowed in the URI syntax, so your desired result is not 100% possible. You can encode and decode them though - Graham Walters
$cat1 + $url_vars
should give the right output when combined withhttp_build_query()
- Ja͢ck@m59 not everyone has php5.4 - Ja͢ck
Btw could you do
var_dump($cat1);
? It's somewhat ambiguous now. - Ja͢ck