Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-重新组织查询字符串参数_Php_Arrays - Fatal编程技术网

PHP-重新组织查询字符串参数

PHP-重新组织查询字符串参数,php,arrays,Php,Arrays,假设我有一个如下的查询字符串: ?foo1bar1=a&foo1bar2=b&foo1bar3=c&foo2bar1=d&cats1dogs1=z $indexes = array('foodex', 'bardex'); $columns = array('foo<foodex>bar<bardex>', 'cats<foodex>dogs<bardex>'); 此字符串中的参数可以是任意的,并且可以有任意数量

假设我有一个如下的查询字符串:

?foo1bar1=a&foo1bar2=b&foo1bar3=c&foo2bar1=d&cats1dogs1=z
$indexes = array('foodex', 'bardex');
$columns = array('foo<foodex>bar<bardex>', 'cats<foodex>dogs<bardex>');
此字符串中的参数可以是任意的,并且可以有任意数量的索引(因此您可以只使用
foo=
,也可以使用
foo1bar1=
或类似的
foo1bar1baz1=
。但是,参数及其相关索引将提前知道

我希望能够获取此查询字符串,再加上一个配置,并重新构造它…配置可能如下所示:

?foo1bar1=a&foo1bar2=b&foo1bar3=c&foo2bar1=d&cats1dogs1=z
$indexes = array('foodex', 'bardex');
$columns = array('foo<foodex>bar<bardex>', 'cats<foodex>dogs<bardex>');
$index=array('foodex','bardex');
$columns=数组('foobar','catsdogs');
所需的输出将是“列”重新组织成由适当索引索引的行,准备存储在数据库行中

array(
    array(
        'foodex' => 1,
        'bardex' => 1,
        'foo<foodex>bar<bardex>' => 'a',
        'cats<foodex>dogs<bardex>' => 'z'
    ),
    array(
        'foodex' => 1,
        'bardex' => 2,
        'foo<foodex>bar<bardex>' => 'b',
        'cats<foodex>dogs<bardex>' => null
    ),
    etc.
)
数组(
排列(
'foodex'=>1,
“bardex”=>1,
'foobar'=>a',
“catsdogs”=>“z”
),
排列(
'foodex'=>1,
“bardex”=>2,
'foobar'=>'b',
“catsdogs”=>null
),
等
)
我想出了几个解决这个问题的办法,但似乎没有什么特别优雅的……我可以:

  • 编写一个递归函数,循环一个已知索引的所有可能值,然后调用自己循环下一个已知索引的所有可能值,然后记录结果。这将非常慢…您可能循环数千或数百万个可能的索引值,但只在查询字符串中找到一小部分
  • 循环查询字符串中的每个实际值,执行某种正则表达式检查,查看它是否与我要查找的列之一匹配,包括其中列出的每个索引的通配符。然后,我可以使用索引构建某种多维数组,并最终为输出展平它。这将运行得更快,但eems非常复杂

  • 有没有一个优雅的解决方案摆在我面前?我很想听听你的建议。

    这是你可以开始的快速示例:

    // your configuration
    $indexes = array ('foodex', 'bardex');
    $columns = array ('foo<foodex>bar<bardex>', 'cats<foodex>dogs<bardex>');
    
    // column names converted into regexps
    $columns_re = array_map ( function ($v) {
        global $indexes;
        return '/^' . str_replace ( array_map ( function ($v) {
            return '<' . $v . '>';
        }, $indexes ), '(\d+)', $v ) . '$/';
    }, $columns );
    
    // output array
    $array = array ();
    foreach ( $_GET as $key => $value ) {
        foreach ( $columns_re as $reIdx => $re ) {
            $matches = array ();
            if (preg_match_all ( $re, $key, $matches )) {
                // generate unique row id as combination of all indexes
                $rowIdx = '';
                foreach ( $indexes as $i => $idxName )
                    $rowIdx .= $matches [$i + 1] [0] . '_';
                // fill output row with default values
                if (! isset ( $array [$rowIdx] )) {
                    $array [$rowIdx] = array ();
                    foreach ( $indexes as $i => $idxName )
                        $array [$rowIdx] [$idxName] = $matches [$i + 1] [0];
                    foreach ( $columns as $name )
                        $array [$rowIdx] [$name] = null;
                }
                // fill actually found value
                $array [$rowIdx] [$columns [$reIdx]] = $value;
            }
        }
    }
    
    //您的配置
    $index=array('foodex','bardex');
    $columns=数组('foobar','catsdogs');
    //转换为regexp的列名
    $columns\u re=数组\u映射(函数($v){
    全球美元指数;
    返回“/^”.str\u replace(数组映射)(函数($v){
    返回“”;
    },$index),“(\d+”,$v)。“$/”;
    },$列);
    //输出阵列
    $array=array();
    foreach($\u获取为$key=>$value){
    foreach($reIdx=>$re列){
    $matches=array();
    if(预匹配所有($re,$key,$MATCHS)){
    //生成唯一的行id作为所有索引的组合
    $rowIdx='';
    foreach($i=>$idxName的索引)
    $rowIdx.=$matches[$i+1][0]。';
    //用默认值填充输出行
    if(!isset($array[$rowIdx])){
    $array[$rowIdx]=array();
    foreach($i=>$idxName的索引)
    $array[$rowIdx][$idxName]=$matches[$i+1][0];
    foreach($列作为$name)
    $array[$rowIdx][$name]=null;
    }
    //填充实际找到的值
    $array[$rowIdx][$columns[$reIdx]]]=$value;
    }
    }
    }
    

    通过PHP5.3测试,经过一些修改后,可以在任何版本下运行

    ,因此您想在SQL数据库中存储URL查询?什么是
    foobar
    ?您想做什么?您能解释一下您的目标吗?请参阅以供参考。我想在SQL数据库中存储查询字符串参数,但这很简单。这里的问题是关于rest的结构化数据。给定一组参数,这些参数可能是同一参数的多个实例(
    foo1=abc
    foo2=def
    ),我想为索引的每个值生成一行,并将参数放在列中。即使这样也没那么糟糕,只是我需要能够接受任意数量的索引。因此,您可以有一个包含3个索引的参数(
    foo1bar1baz1=abc
    )或者更多……你可能会发现这很有趣:基本上是选项2。谢谢!知道没有一个简单得多的算法盯着我看,这有点让人欣慰。我会继续接受它,因为它回答了问题,没有其他人提供了更好算法的建议。谢谢!