I'm building a filters section for my search page and I was wondering what is the best way to go about doing the query strings. My problem is that these links function a lot like checkboxes, so some, all or none can be on. I'd have to loop through each of about 30 or so link开发者_C百科s, removing or adding that specific link's value depending on its state.
My first concern is:
Should I pass the arrays like
colors=red,blue,green //explode?
or
colors[]=red&colors[]=blue&colors[]=green //parse_str?
What is the fastest/best way to remove a certain value of a certain array as I loop through each link? I imagine it'd be a bit more complicated using the second method I've posted above, yes?
EDIT2 - What do you think of this?
I've ran into a tutorial online and came up with this:
function remove( $filters = array(), $remove_key = NULL, $remove_val = NULL ) { if( $remove_key != NULL && array_key_exists($remove_key,$filters) ) { if( $remove_val != NULL && array_key_exists($remove_val,array_flip($filters[$remove_key])) ) { $filters[$remove_key] = array_diff($filters[$remove_key],array($remove_val)); } else { unset($filters[$remove_key]); } } return http_build_query( $filters ); }
Currently, I can pass $remove_key to remove a key and $remove_val to remove a value from a key in an array.
What do you guys think? Would this be too slow for for doing 30-50 links? Thanks!
colors[]=red&colors[]=blue&colors[]=green
Would be a the Way a Form would submit the data (when method="get"). And you can access it via $_GET['colors'] which is the native and by that probably the fastest way.
EDIT: to get that string via http_build_query
just fill them in the array color
$data = array('colors' => array('green','red','blue'));
echo http_build_query($data); // colors[0]=green&colors[1]=red&colors[2]=blue
精彩评论