0

My array is something different from normal cakephp array. now i want to save this data array using foreach loop.

[Data] => Array
        (
            [type_id] => Array
                (
                    [0] => 4
                    [1] => 5
                    [2] => 4
                    [3] => 3
                )

            [data_id] => Array
                (
                    [0] => 1
                    [1] => 3
                    [2] => 2
                    [3] => 1
                )

            [price] => Array
                (
                    [0] => 12
                    [1] => 213
                    [2] => 212
                    [3] => 23343
                )

            [amount] => Array
                (
                    [0] => 121
                    [1] => 342
                    [2] => 45454
                    [3] => 3243
                )

        )

i just want to a loop of foreach.

2 Answers 2

2

You want to restructure your array first and then use saveMany() to save the restructured array:-

$data = array();
foreach ($array['ProductCharge'] as $col => $value) {
    foreach ($value as $key => $val) {
        $data[$key]['ProductCharge'][$col] = $val;
    }
}

$this->ProductCharge->saveMany($data);

It is better to use saveMany() than save each record in a foreach loop.

There is possibly a Hash method for restructuring the data without the need for the foreach loops, but can't think which would work as desired.

Sign up to request clarification or add additional context in comments.

1 Comment

surely Hash method magic w'd have done justice here. your answer looks somehow opaque. good one nontheless
0

I don't know which approach you've tried so far.

But $this->Model->saveMany($arrayVariable) should be just ok to save your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.