1

I am new in PHP. I am working for send data using PHP API. Its method is like below

$createContact->setCustomFieldValues(
                    [
                       array(
                        'customFieldId' => 'pcVFD6',
                        'value' => array('35')
                        ),
                        array(
                        'customFieldId' => 'pcVnzW',
                        'value' => array('37')
                        )
                    ]
            );

I have data in array like this

$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");

I want pass this values to above function but I am not getting idea about proper way to do it. I have tried something like this

foreach ($aa as $key => $value){
                
    $createContact->setCustomFieldValues([
        array(
            'customFieldId' => $key,
            'value' => array($value)
            )  
        ]
    );
}

its working but passing only last one array to API. Anyone here can please help me for achieve my goal?

Thanks!

1 Answer 1

2

You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.

For example:

$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();

foreach ($aa as $key => $value){
  $transformedArr[] = array(
            'customFieldId' => $key,
            'value' => array($value)
            );
}

$createContact->setCustomFieldValues($transformedArr);
Sign up to request clarification or add additional context in comments.

1 Comment

@HinaPatel No worries. Just a friendly note - not everyone here is a "sir" or wishes to be thought of as one. I'm not offended personally, but for the future it's better to use neutral language or just refer to people by their usernames, so that you don't get it wrong by accident :-)

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.