'Insert multidimensional array to codeigniter cart

I'm building online store using codeigniter and it's cart library. I have this array

$data = Array
(
    Array
        (
            [id] => BM101
            [qty] => 1
            [price] => 95
            [name] => Legal Structures & Tax Accounting
            [options] => Array
                (
                    [course_hours] => 4
                    [course_description] => This course will review the alternative legal forms that may be used to conduct a building/construction business, including proprietorship, partnerships, joint ventures, limited partnerships, C corporations and S corporations. The pros and cons of each type of entity will be examined in terms of personal liability exposure. The income tax and social security tax consequences of each type of entity, as well as the impact on fringe benefit and retirement programs, will be explored.
                    [class_date] => 2014-03-05
                    [class_time] => 1:00 p.m. - 4:30 p.m.
                    [class_instructor] => Richard Allison
                )

        )

    Array
        (
            [id] => BM102
            [qty] => 1
            [price] => 95
            [name] => Risk Management for Residential General Contractors
            [options] => Array
                (
                    [course_hours] => 4
                    [course_description] => This course will provide a risk management approach to the most common property and liability exposures facing the residential general contractor. A broad review of the major coverage’s builders must have to protect their businesses will be covered. Those coverage’s include: general liability, workers compensation, builders risk, and auto. Other items discussed will be include: contractor tools, office contents, and bonds. This will be a practical approach on how to assess these risks and how to insure them or pass them along to others.
                    [class_date] => 2014-04-03
                    [class_time] => 8:30 a.m. - 12:00 p.m.
                    [class_instructor] => Erik Anderson
                )

        )

)

When I insert using $this->cart->insert($data) only the last item inserted to cart. how to insert multidimensional array to cart? I also do with looping

foreach ($data as $item){
$this->cart->insert($item);
}

also only insert the last item.



Solution 1:[1]

I think this is related to the ampersand in the name field of the 1st item. This Stack Overflow answer should have some more info on getting around that

Solution 2:[2]

instead of looping in CI, a better way is to use:

$this->db->insert_batch('table_name', $data)

insert_batch() is used for inserting many items in a multidimensional array at once.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 ReNiSh AR