Adam Catlow
Adam Catlow

Reputation: 35

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

Upvotes: 0

Views: 290

Answers (3)

Al-Amin
Al-Amin

Reputation: 656

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;
}

Upvotes: 0

RisingSun
RisingSun

Reputation: 1733

tags is not an associated array. It is just a list of strings. Also, look at the value of $existingParams = request()->query(); It is not the tags array. It is an object that contains it. That is why when you use $key it works but deletes everything because $key is tags. So, in your check $existingParams['tags'] should be checked for the shrubs value. in_array is what you are looking in this case.

Upvotes: 1

nnikolay
nnikolay

Reputation: 1751

I hope this solution will help you:

<?php
function remove_query_params(array $params = [])
{
    //Set to array
    $existingParams = [
        'tags' => [
            'aaaa',
            'bbbb'
        ],
        'category' => 'ccc'
    ];

    // go trough all parameters
    foreach ($existingParams as $key1 => $value1) {
        // go to the parameters, which need to be deleted
        foreach ($params as $key2 => $value2) {
            // only if the keys equals, do something
            if ($key1 === $key2) {
                // if the param is an array
                if (is_array($value1)) {
                    foreach ($value1 as $k => $v) {
                        // if the elements to delete are an array
                        if (is_array($value2)) {
                            foreach ($value2 as $b => $r) {
                                if ($v == $r) {
                                    unset($existingParams[$key1][$k]);
                                }
                            }
                        } else {
                            if ($v == $value2) {
                                unset($existingParams[$key1][$k]);
                            }
                        }
                    }
                } else {
                    if (isset($existingParams[$key2])) {
                        unset($existingParams[$key2]);
                    }
                }
            }
        }
    }
    $query = http_build_query($existingParams);
    return $query;
}

echo remove_query_params(['tags' => 'aaaa']);
echo "\n";
echo remove_query_params(['tags' => ['aaaa', 'bbbb']]);
echo "\n";
echo remove_query_params(['category' => 'ccc']);
echo "\n";

Upvotes: 1

Related Questions