Support

Account

Home Forums Backend Issues (wp-admin) Unique combinations from two checkbox fields

Solved

Unique combinations from two checkbox fields

  • I have two checkbox fields:

    AAA (with the choices A, B, C)
    BBB (with the choices 10, 20, 30)

    In made in the front end the followoing choices:

    In AAA: A, B
    In BBB: 20, 30

    Now I want to calculate the unique combinations, so I want (as text): A20, B20, B20, B30, also A20 B20 B20 B30 will do, the comma is not needed.
    I made this:

    function class_tijd($post_id)
    {
        $class = get_field('AAA');
        $tijd = get_field('BBB');
        
       foreach ($class as $value1) {
        	$value1 = implode(' ', $class);
        		foreach ($tijd as $value2) {
        			
        			$value2 = implode(' ', $tijd);
        			
        		}
        	$value = $value1 . $value2;	
        }
        
        $field_name = "classtijd";
        update_field($field_name, $value, $post_id);

    The result I get is: A B20 30

    I found out that I could solve yhis with possible array_merge, but can not find the right way to get it done. Any ideas?

  • The only way to do this is to loop through one array and a nested loop through the second array and concatenate each value individually. There is no single php function that will automatically do this work.

    
    $class = get_field('AAA');
    $tijd = get_field('BBB');
    
    $value = array();
    foreach ($class as $a) {
      foreach ($tijd as $b) {
        $value[] = $a.$b;
      }
    }
    $value = implode(', ', $value);
    
  • Thanks a lot, that was the solution I was looking for!

    Peter

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Unique combinations from two checkbox fields’ is closed to new replies.