[英]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.
“add_query_params”(感谢本论坛)运行良好,我现在可以向相同类型的查询字符串添加多个标签。
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
.因此,如果我要从上述 URL 中删除 say
$tags[]shrubs
,它实际上会同时删除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.这不起作用,如果我将 $value 更改为 $key 那么它会起作用,但它会删除相同类型的所有键,而不是我想要的行为。
I activate this behaviour via a call in the blade template, this forms a href我通过刀片模板中的调用激活此行为,这形成了一个 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亚当
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";
tags
is not an associated array. tags
不是关联的数组。 It is just a list of strings.它只是一个字符串列表。 Also, look at the value of
$existingParams = request()->query();
另外,看看
$existingParams = request()->query();
It is not the tags
array.它不是
tags
数组。 It is an object that contains it.它是一个包含它的对象。 That is why when you use
$key
it works but deletes everything because $key
is tags
.这就是为什么当您使用
$key
它可以工作但会删除所有内容,因为$key
是tags
。 So, in your check $existingParams['tags']
should be checked for the shrubs
value.因此,在您的支票
$existingParams['tags']
应检查shrubs
值。 in_array
is what you are looking in this case. in_array
就是您在这种情况下要查找的内容。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.