Combinando 2 matrices para que se muestren correctamente en mi URL

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!!

preguntado el 10 de septiembre de 13 a las 00:09

Just a suggestion - stop using array() y use [] preferiblemente. array() is painful on the eyes. -

Square bracket characters are not allowed in the URI syntax, so your desired result is not 100% possible. You can encode and decode them though -

$cat1 + $url_vars should give the right output when combined with http_build_query() -

@m59 not everyone has php5.4 -

Btw could you do var_dump($cat1);? It's somewhat ambiguous now. -

1 Respuestas

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 or haz tu propia pregunta.