ranslobo ranslobo - 14 days ago 5
PHP Question

PHP Build url query from array

I'm trying to build URL query from an Array that looks like that:

$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];


I would like to get query like that:

localhost/get?serial=3804689&serial=3801239&serial=3555689


(you get the idea)

I'm trying to use
http_build_query($serials, 'serial', '&');
but it adds the numeric index to the prefix 'serial'.

Any idea how to remove that numeric index?

Answer

Maybe use a foreach:

$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
    $get .= "&serial=$serial;
}
Comments