将数组从模型组合并返回到控制器 - CodeIgniter

Posted

技术标签:

【中文标题】将数组从模型组合并返回到控制器 - CodeIgniter【英文标题】:Combining and returning arrays from Model to Controller - CodeIgniter 【发布时间】:2013-02-15 06:27:00 【问题描述】:

我想组合来自两个单独查询的两个数组。我将我的产品存储在两个单独的表中:一个用于一般信息,如名称、描述、价格等;另一个是他们的各种细节,即服装的尺寸和颜色。

所以我的产品数据库结构如下:

products table:
p_id | title | descr | etc

product_attrs table:
attr_id | products.p_id | name | value

其中 name 和 value 可以是 name = Size value = Large

如果我尝试在一个查询中获取产品的所有详细信息,如下所示:

this->db->select('products.title,
                           p.description,
                           p.price,
                           p.stock,
                           p.name,
                           p.value');
        $this->db->from('p');
        $this->db->where('p.p_id', $id);
        $this->db->join('product_attrs', 'product_attrs.product_id = p.p_id', 'inner');
        $result = $this->db->get();

        return $result->result_array();

我得到一个数组,其中填充了该产品的 product_attributes 表中的名称/值对的数量。所以如果一个产品有 5 个属性,我会像这样把所有的东西都拿回来 5 次:

Array ( [0] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Brand [value] => Modestly Active Swimwear ) [1] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => Colour [value] => Black and Light Blue ) [2] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => small ) [3] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => medium ) [4] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 [name] => size [value] => large ) )

所以我决定将每个表的查询分开,这样我就可以获得一个结果集。但我想将它们结合起来,这样我就可以将数据作为一个数组返回给控制器并将其显示到我的视图上。这就是我查询这两个表的方式,我只需要一种方法来组合两者的结果:

$this->db->select('p.title,
                           p.description,
                           p.price,
                           p.stock');
        $this->db->from('p');
        $this->db->where('p_id', $id);
        $result = $this->db->get();

        $this->db->select('name, value');
        $this->db->from('product_attrs');
        $this->db->where('p_id', $id);
        $result2 = $this->db->get();

如果有人可以帮忙,我将不胜感激。谢谢

编辑:

我现在正在 php.net 中查看 array_merge() 函数,但如果我这样做:

$result = $this->db->get();
        $array1 = $result->result_array();

$result2 = $this->db->get();
        $array2 = $result2->result_array();
        $data = array_merge($array1,$array2);
        return $data;

我得到了多个数组:

Array ( [0] => Array ( [title] => Modest Swimsuit - Full body [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant [price] => 59.95 [stock] => 20 ) [1] => Array ( [name] => Brand [value] => Modestly Active Swimwear ) [2] => Array ( [name] => Colour [value] => Black and Light Blue ) [3] => Array ( [name] => size [value] => small ) [4] => Array ( [name] => size [value] => medium ) [5] => Array ( [name] => size [value] => large ) )

在我看来,有没有办法从上述数组中获取值?

【问题讨论】:

我想我明白了。我使用函数 array_merge(),然后在我看来,我可以得到如下值:echo $product_details[0]['title'];。感谢上帝的 var_dump! OP 试图编辑答案:该表的前一个名称非常通用,所以我没有看到任何问题。你没有编辑所有的事件是吗?你编辑了你的问题,不管怎样,但你仍然有.description 的问题,并尝试编辑.desc 的答案。不会再匹配了。 @FelipeAls 我认为这就是重点:/。通用是一件好事吗? 【参考方案1】:

另一种选择是遍历第二个结果数组并将值添加到第一个数组中。

    // Get products results as an array
    $this->db->select('products.title,
                       products.description,
                       products.price,
                       products.stock');
    $this->db->from('products');
    $this->db->where('product_id', $id);
    $product = $this->db->get()->result_array();

    // Get product attributes as an array
    $this->db->select('name, value');
    $this->db->from('product_attributes');
    $this->db->where('product_id', $id);
    $product_attributes = $this->db->get()->result_array();

    // Loop through the results
    foreach($product_attributes as $attribute) 

         // Add results to original product array
         $product[$attribute['name']] = $attribute['value'];
    

这应该产生一个像这样的数组:

        [title]       => Modest Swimsuit - Full body 
        [description] => UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea Water Resistant 
        [price]       => 59.95 
        [stock]       => 20
        [Brand]       => Modestly Active Swimwear 
        [Colour]      => Black and Light Blue
        [size]        => small

【讨论】:

这是一个更清晰的结果,我会尝试一下。感谢您的帮助:)

以上是关于将数组从模型组合并返回到控制器 - CodeIgniter的主要内容,如果未能解决你的问题,请参考以下文章

二路归并排序(也叫合并排序)

是否可以在不使用表单的情况下将数组从树枝发送到控制器?

将数组从控制器传递到 codeigniter 中的模型

将数组从视图控制器传递到视图控制器返回空数组

如何在 jQuery 中将 2 个数组合并为 1 个数组? [复制]

php怎么取出一组数组中每个下标下的最后一个元素