0

I'm trying to pass a multi dimensional array to a database table and columns, but im getting stuck in a place that i cannot understand how can i put the foreach function to get the correct result.

Below my data:

data:

array(5) {
        ["option_id"]=>  int(115)

  ["name"]=>
          array(3) {
            [0]=> string(1) "S"
            [1]=> string(1) "M"
            [2]=> string(1) "L"
          }
  ["value"]=>  array(3) {
            [0]=> string(5) "12345"
            [1]=> string(5) "12346"
            [2]=> string(5) "12347"
        }
  ["price"]=>  array(3) {
            [0]=> string(3) "199"
            [1]=> string(3) "199"
            [2]=> string(3) "199"
        }
  ["inventory"]=>  array(3) {
    [0]=> string(1) "1"
    [1]=> string(1) "1"
    [2]=> string(1) "1"
  }
}

each string from name, value, price, inventory need to be saved in each row in database data from database

but my problem is that on create the foreach to store the data in database it store only the name on all columns.

The model im using to save the data is:

function saveBatchOptionsValues($option_values){
        $sequence   = 0;
        foreach($option_values['name'] as $value){

            $values['option_id'] = $option_values['option_id'];
            $values['name'] = $value['name'];
            $values['value'] = $value['value'];
            $values['sequence'] = $sequence;
            $values['inventory']    = $value['inventory'];
            $values['weight']   = floatval(1.00);
            $values['price']        = floatval($value['price']);
            $sequence++;
            $this->db->insert('option_values', $values);
        }

    }

The question is how can i pass the columns value, price, inventory etc.. to each row?

Any help is appreciated !

1
  • This looks like it should probably have a codeigniter tag Commented Jan 9, 2015 at 18:26

2 Answers 2

1

If you iterate over the name subarray you only will get the name as expected. For getting rows this is how you can iterate over your data structure. You have to access the different values from the different rows by an numeric index because they are spread over different arrays (Instead of iterating with foreach):

function saveBatchOptionsValues($option_values){
    $count = count($option_values["name"]);
    for($i = 0; $i < $count; $i++) {
        $values['option_id'] = $option_values['option_id'];
        $values['name'] = $option_values['name'][$i];
        $values['value'] = $option_values['value'][$i];
        $values['sequence'] = $i;
        $values['inventory'] = $option_values['inventory'][$i];
        $values['weight']   = floatval(1.00);
        $values['price'] = floatval($option_values['price'])[$i];
        $this->db->insert('option_values', $values);
    }
}

Brief explanation

I've changed the example to fit yours. You have arrays of arrays right? In the subarrays are the same kind of data, e.g. names. So all names are stored in the same array. What happens if you iterate over exact that sub array? In each iteration you retrieve name. So if you are e.g. in the second iteration, you retrieve the name. But now you also want to have the price for that exact name / product. Thats not possible by using the foreach loop because with foreach you can iterate over just one array. So what you really want to do is to iterate over multiple arrays simultaneously. You can achieve that using a for loop where a counter is increased. With that counter you can access each of the different arrays (names, prices etc.) in the same iteration. I hope it has become a bit clearer.

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

3 Comments

That's it, it do the job :)
No problem. I've added an explanation for better understanding. Perhaps you understand it better that way.
Perfect, the idea is that all the time Name will contain the same numbers of items for Value, Inventory, Price and Weigth, so Size S is name, value 1234 is the Code, 199 is the Price, 1 Inventory, and so on, never will be 2 items on value and 3 on name, because one size will not have a Code. Thanks
0

Your problem is in how you're using the foreach function

foreach($option_values['name'] as $value){

You need to do something like:

foreach($option_values as $value){

2 Comments

right, but it will get name, value, price and inventory in array values, so in this case will be useful insert_batch function from codeigniter db ?
This does not help him to get his data as rows. He wants name(0) together with price(0) and inventory(0) etc.

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.