使用CodeIgniter ruri_to_assoc()保存值数组

z8dt9xmd  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在构建一个图库,它将使用URI来定义一组过滤器设置。CodeIgniter的ruri_to_assoc()非常适合我的大多数设置,因为它们就像key=〉value一样简单。然而,一个值(标记)可以包含我希望匹配的多个值。
由于ruri_to_assoc()使用键/瓦尔对,我如何将数组设置为url中的键?例如:
/天/365/订单/最旧/标签/汽车/标签/红色/标签/野马
现在,看起来我必须分解uri_string()并自己循环通过它。

$test_fil = explode('/',$this->uri->uri_string());
        unset($test_fil[0]);
        $val = 'key';
        foreach($test_fil as $fkey=>$fval){
            if($fval=='tags'){
                $val = 'tag';
                $new_filter['tags'] = '';
            }
            else{
                if($val == 'key'){
                    $new_filter[$fval] = '';
                    $val = 'val';
                    $current_key = $fval;
                }
                elseif($val == 'val'){
                    $new_filter[$current_key] = $fval;
                    $val = 'key';
                }
                else{
                    $new_filter['tags'][] = $fval;
                }

            }
        }

CI中有什么可以为我做到这一点吗?

kjthegm6

kjthegm61#

我们只需要确保uri_to_assoc在构建数组时不会覆盖已经存在的键。
在 * 应用程序/核心 * 中创建MY_URI.php

class MY_URI extends CI_URI
{
    protected function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
    {
        if ( ! is_numeric($n))
        {
            return $default;
        }

        if (isset($this->keyval[$which], $this->keyval[$which][$n]))
        {
            return $this->keyval[$which][$n];
        }

        $total_segments = "total_{$which}s";
        $segment_array = "{$which}_array";

        if ($this->$total_segments() < $n)
        {
            return (count($default) === 0)
                ? array()
                : array_fill_keys($default, NULL);
        }

        $segments = array_slice($this->$segment_array(), ($n - 1));
        $i = 0;
        $lastval = '';
        $retval = array();

        foreach ($segments as $seg)
        {
            /*HERE IS THE PART WE TRULY OVERRIDE*/
            if ($i % 2)
            {
                if(is_array($retval[$lastval]))
                    $retval[$lastval][] = $seg;
                else
                    $retval[$lastval] = $seg;
            }
            else
            {
                if(isset($retval[$seg]) && !is_array($retval[$seg]))
                {
                    $tmp = $retval[$seg];
                    $retval[$seg] = array();
                    $retval[$seg][] = $tmp;
                }   
                else if(!isset($retval[$seg]))
                {
                    $retval[$seg] = NULL;
                    $lastval = $seg;
                }
            }
            /*END OVERRIDE*/

            $i++;
        }

        if (count($default) > 0)
        {
            foreach ($default as $val)
            {
                if ( ! array_key_exists($val, $retval))
                {
                    $retval[$val] = NULL;
                }
            }
        }

        // Cache the array for reuse
        isset($this->keyval[$which]) OR $this->keyval[$which] = array();
        $this->keyval[$which][$n] = $retval;
        return $retval;
    }
}

然后在你的控制器里,什么也没改变:

$myTags = $this->uri->uri_to_assoc();

下面是我测试后得到的结果:

array (size=3)
  'days' => string '365' (length=3)
  'order' => string 'oldest' (length=6)
  'tag' => 
    array (size=3)
      0 => string 'car' (length=3)
      1 => string 'red' (length=3)
      2 => string 'mustang' (length=7)

相关问题