Skip to content
Advertisement

Removing an associated key/value pair from array (nested)

I have two function to add remove parameters to the query string. The “add_query_params” (thanks to this forum) is working nicely and I can now add multiple tags to the query string of the same type.

For example

http://example.com?tags[]=flowers&tags[]=shrubs&category[]=garden

As you can see, I can add multiple of the same parameters, I am also querying these nicely using queryfilters.

However my newest problem, is simply removing a single tag type without affecting the rest of the query string. I will then rebuild the query without the deleted tag.

Someone kindly yesterday helped me to to a point but this removes ALL tag key values, not just the specified tag.

So if I was to delete say $tags[]shrubs from the above URL it would actually delete BOTH tag[]shrubs AND $tags[]flowers.

This obviously isn’t very intuitive for a filter system I am devising. What I would like to know is how to remove just the single key value pair and leave the other keys pairs intact.

Here is my helper function

//Accept a param array which passthrough through tag type eg category/tag and value
function remove_query_params(array $params = [])
{
    //Set to array
    $existingParams = [];

    $existingParams = request()->query();

    foreach($params as $key=>$value){

        if (isset($existingParams[$value])) {
            unset($existingParams[$value]);
        }
    }


    $query = http_build_query($existingParams);

    return url()->current() . '?' . $query;
}

//Need to return: user removes tag from filter in blade, URL recontructs without the passed through tag value
//Before
//http://example.com?tags[]=flowers&tags[]=shrubs&category[]=garden

//After
//http://example.com?tags[]=flowers&category[]=garden

This does not work, if I change $value to $key then it will will, but it will remove all keys of the same type, not the behaviour I would like.

I activate this behaviour via a call in the blade template, this forms a href

//Pass through parameter type and parameter value
{{remove_query_params(['category' => $category->id]) }}

Has anybody got any pointers as to where I go next?#

Thanks and fingers crossed I am not far off 🙂

Adam

Advertisement

Answer

Hope this will solve your problem.I just provided the core function to get the things done in a way

$query = "tags[]=flowers&tags[]=shrubs&category[]=garden";
echo (remove_query_params( [ 'tags' => 'shrubs' ], $query ));


function remove_query_params(array $params = [], $query )
{
    parse_str( $query, $existingParams );
    
    $existing_keys = array_keys( $existingParams);
    
    foreach($params as $key=>$value){
        if( in_array( $key, $existing_keys ) ){
           foreach ($existingParams[$key] as $param_key => $param_value) {
               if( $param_value == $value ){
                unset( $existingParams[$key][$param_key] );
               }
           }
        }
    }
    $query = http_build_query($existingParams);
    return $query;
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement