1

I have the following problem. I have these types of select inside my form:

<select name="variation[8][]" multiple>
   <option value="1">s</option>
   <option value="2">m</option>
   <option value="3">l</option>
</select>

<select name="variation[9][]" multiple>
   <option value="8">red</option>
   <option value="9">blue</option>
   <option value="10">black</option>
</select> 

I want to read the posted array properly with php and I want to write this in the database:

variation_id => 8 
variation_term => 1 
variation_id => 8 
variation_term => 2 
variation_id => 8 
variation_term => 3 

variation_id => 9 
variation_term => 8 
variation_id => 9 
variation_term => 9 
variation_id => 9 
variation_term => 10

How can I parse this array posted? There is a better way to do this?

TNX

I have found this solution:

foreach ($_POST['variation'] as $key => $value) {
   for($i=0; $i<count($value); $i++){
     echo "varitaion name = $key"."<br>";
     echo "value = $value[$i]";
   }
}
2
  • DO you want to fetch the values from POST array? Commented Mar 1, 2014 at 16:20
  • yes but no a simple post array name="variable[]" but from name="variable[8][]" because I need to know VALUES of that select and the select variable[NAME]. So in my database I will write the name of the variant => 8 Example. variant=>8 value=>1 variant=>8 value=>2 and if i have another SELECT variant=>9 value=>10 variant =>8 value=>15 Commented Mar 1, 2014 at 16:26

1 Answer 1

1

Try

foreach($_POST['variation'] as $key=>$val){
  if(is_array($val)){
     echo " Values for $key Are:"; // get your variation index eg:8,9 etc
     foreach($val as $key1=>$val1){
         echo $val1.","; // get the index values
     }
         echo "<br/>";
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works perfectly! I made a similar solution I have edited my question! thanks so much!

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.