Support

Account

Home Forums Bug Reports Update field for multiple choose with array

Solving

Update field for multiple choose with array

  • Hello,

    i got some issue with the function update_field()

    How update multiple value with include the array “value/label”.

    Example :

    
            // first array from old get_field, return this structure
    	$arr = array(
    			  "0" => array(
    			    "value" => "test1",
    			    "label" => "Test 1"
    			   ),
    			  "1" => array(
    			    "value" =>"test2",
    			    "label" => "Test 2"
    			  )
    		  );
    
    	echo '<pre>';
    		echo var_dump($arr);
    	echo '</pre>';
    
    	update_field('field_test', $arr, $post_id);
    
    	$arr2 = get_field('field_test', $post_id);
    
    	echo '<pre>';
    		echo var_dump($arr2);
    	echo '</pre>';
    
    

    return :

    array(2) {
      [0]=>
      array(2) {
        ["value"]=>
        string(5) "Array"
        ["label"]=>
        string(5) "Array"
      }
      [1]=>
      array(2) {
        ["value"]=>
        string(5) "Array"
        ["label"]=>
        string(5) "Array"
      }
    }

    Thanks for help

  • Ok i find the issue,

    for valid the update field, we must return only the value (get_field without format), like :

    	$arr = array(
    			  "0" => "test1",
    			  "1" => "test2"
    		  );

    Then the core ACF will save the field correctly with value.

    But, i use the get_fields() in my API, then the developper of APP from my WordPress, return all fields with value/label, so i must modified the core of ACF.

    like :
    includes\fields\class-acf-field-select.php – line 526

    		
    if( is_array($value) ) {
        //$value = array_map('strval', $value); // OLD // remove array and up the value in parent array
    
        // will print the array and select only the value
        foreach ($value as $key => $val) {
            // check if have more 2 characters, some field return 1 letter :/
            if ( strlen($val['value']) > 2 ) {
    		$value[$key] = $val['value'];
    	} else {
    		$value = array_map('strval', $value);
    	}
        }
    }
    

    That quick code is used for select field and checkbox field.

    Can you add some filter for help developper ?

    thanks,
    Best Regards, Mat.

  • Hello,

    i clean the code

    		
    if( is_array($value) ) {
    
    	foreach ($value as $key => $val) {
    
    		//echo var_dump($val);
    		if ( isset($val['value']) ) {
    			$value[$key] = $val['value']; // when value is available, like return value/label with API in json
    		} else {
    			$value = array_map('strval', $value); // will get directly the parent array
    		}
    
    	}
    
    }
    

    @team, do you need more informations for apply this fix to ACF ?

    Thanks

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

You must be logged in to reply to this topic.