Support

Account

Home Forums General Issues How to update user field with correct object etc Reply To: How to update user field with correct object etc

  • Ok, I’ve been hacking away and I’ve got the updating of the arrays working perfectly, but it’s not saving data correctly – I’m seeing that the data is serialised, so other than saving a new array, I’m not sure how this should be done?

    // retrieved variables from Ajax request
    $user = $_REQUEST['user'];
    $decision = $_REQUEST['decision'];
    $decision_id = $_REQUEST['decision_id'];
    
    // get current decision info
    $this_decision = get_field('decision_makers', $decision_id);
    // echo '<pre>';print_r(array_search($user, array_column($this_decision, 'ID')));echo '</pre>';;
    
    // store the changed user in a var
    $moved_user = $this_decision[array_search($user, array_column($this_decision, 'ID'))];
    // echo '<pre>';print_r($moved_user);echo '</pre>';
    
    // remove user from decision_makers
    unset($this_decision[array_search($user, array_column($this_decision, 'ID'))]);
    echo 'new updated decision';
    echo '<pre>';print_r($this_decision);echo '</pre>';
    update_field('field_575e67c05fc55', $this_decision, $decision_id);
    
    // add user to either decision_yes or decision_no
    switch ($decision) {
    	case 'yes' :
    		$yes_users = get_field('decision_yes', $decision_id);
    		array_push($yes_users, $moved_user);
    		update_field('field_575fee49f5980', $yes_users, $decision_id);
    
    		echo 'new yes users';
    		echo '<pre>';print_r($yes_users);echo '</pre>';
    		break;
    	case 'no' :
    		$no_users = get_field('decision_yes', $decision_id);
    		array_push($no_users, $moved_user);
    		update_field('field_575fee6bf5981', $no_users, $decision_id);
    		
    		echo 'new no users';
    		echo '<pre>';print_r($no_users);echo '</pre>';
    		break;
    }

    It’s working perfectly in that the last user – the gryphon avatar which is me, WAS a part of the first array, but on running this function, it moved me from the ‘new updated decision’ array, into the ‘new yes users’ array…

    Perfect..

    But on trying to save these updated arrays into ACF, it’s borking out:

    update_field('field_575fee49f5980', $yes_users, $decision_id);

    Do I need to handle the serialisation?