如何反转此数组并将重复值用作新数组中的键 [英] How to reverse this array and use duplicate values as key in the new array

查看:76
本文介绍了如何反转此数组并将重复值用作新数组中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使我的代码接受一组过滤器中的多项选择,这是到目前为止通过复选框并选择了元素所得到的结果.

I am trying to make my code accept multiple choices from a set of filters, this is the result I have come to so far through the checkboxes and getting the elements selected.

    Array (
[for-juniors] => product_category 
[for-men] => product_category
[coats] => product_category
[for-women] => product_category
[7-diamonds] => brand ) 

如您所见,传递了多个product_category.我需要了解一个函数或路由,以便能够将该格式转换为代码可以处理的格式.例如

As you see, multiple product_category are passed. I need to know about a function or route to be able to convert that format into format that code can manipulate. E.g.

array('product_category' => 'for-juniors','for-men','coats','for-women', 'brand' => '7-diamonds');

我真的不知道从数组开始的地方.该数组具有重复的值"product_category"作为样本,它表示每个唯一的过滤器,并且键是该过滤器的选择.

I really do not know where to begin with my array. The array has duplicate values "product_category" as sample, this represent each unique filter and keys are the choices of this filter.

任何提示都会有很大帮助.

Any tips will be of great help.

推荐答案

$grouped = array();
foreach ($input as $choice => $group) {
    $grouped[$group][] = $choice;
}

var_dump($grouped);

这篇关于如何反转此数组并将重复值用作新数组中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆