0

I'm trying to search the array and to look for keywords at the second level of the array, like ["Secondary_Volunteering__c"] and Referral_Source_within_CF__c. If those values are found then keep them, if not delete them.

I have created the following but it comes back blank.

Any guidance will be appreciated.

foreach($getAllCustomerCustomFields as $k => $v) {
    if(!in_array(["Secondary_Volunteering__c"], $v) && !in_array("Referral_Source_within_CF__c", $v))
        unset($getAllCustomerCustomFields[$k]);
}

foreach($getAllCustomerCustomFields as $fields){
    foreach($fields as $field){
        echo( '<strong>'.$field["FIELD_LABEL"].'</strong><br>');
        foreach ($field["CUSTOM_FIELD_OPTIONS"] as $cfield){
        echo($cfield["OPTION_VALUE"].'<br>');

        }
         echo('<br>');
    }
   
}

This is the PHP array.

array(1) {
  ["CUSTOMFIELDS"]=>
  array(46) {
    ["Secondary_Volunteering__c"]=>
    array(12) {
      ["FIELD_NAME"]=>
      string(25) "Secondary_Volunteering__c"
      ["FIELD_ORDER"]=>
      int(2)
      ["FIELD_FOR"]=>
      string(7) "CONTACT"
      ["FIELD_LABEL"]=>
      string(22) "Secondary Volunteering"
      ["FIELD_TYPE"]=>
      string(11) "MULTISELECT"
      ["FIELD_HELP_TEXT"]=>
      NULL
      ["DEFAULT_VALUE"]=>
      NULL
      ["EDITABLE"]=>
      bool(true)
      ["VISIBLE"]=>
      bool(true)
      ["CUSTOM_FIELD_OPTIONS"]=>
      array(9) {
        [0]=>
        array(3) {
          ["OPTION_ID"]=>
          int(3)
          ["OPTION_VALUE"]=>
          string(6) "Events"
          ["OPTION_DEFAULT"]=>
          bool(false)
        }
      }
      ["DEPENDENCY"]=>
      NULL
      ["JOIN_OBJECT"]=>
      NULL
    }
    ["Primary_Volunteering__c"]=>
    array(12) {
      ["FIELD_NAME"]=>
      string(23) "Primary_Volunteering__c"
      ["FIELD_ORDER"]=>
      int(3)
      ["FIELD_FOR"]=>
      string(7) "CONTACT"
      ["FIELD_LABEL"]=>
      string(20) "Primary Volunteering"
      ["FIELD_TYPE"]=>
      string(11) "MULTISELECT"
      ["FIELD_HELP_TEXT"]=>
      NULL
      ["DEFAULT_VALUE"]=>
      NULL
      ["EDITABLE"]=>
      bool(true)
      ["VISIBLE"]=>
      bool(true)
      ["CUSTOM_FIELD_OPTIONS"]=>
      array(27) {
        [0]=>
        array(3) {
          ["OPTION_ID"]=>
          int(87)
          ["OPTION_VALUE"]=>
          string(9) "Animal Me"
          ["OPTION_DEFAULT"]=>
          bool(false)
        }
      }
      ["DEPENDENCY"]=>
      NULL
      ["JOIN_OBJECT"]=>
      NULL
    }
    ["Referral_Source_within_CF__c"]=>
    array(12) {
      ["FIELD_NAME"]=>
      string(30) "Referral_Source_within_CF__c"
      ["FIELD_ORDER"]=>
      int(4)
      ["FIELD_FOR"]=>
      string(7) "CONTACT"
      ["FIELD_LABEL"]=>
      string(27) "Referral Source within CF"
      ["FIELD_TYPE"]=>
      string(11) "MULTISELECT"
      ["FIELD_HELP_TEXT"]=>
      NULL
      ["DEFAULT_VALUE"]=>
      NULL
      ["EDITABLE"]=>
      bool(true)
      ["VISIBLE"]=>
      bool(true)
      ["CUSTOM_FIELD_OPTIONS"]=>
      array(8) {
        [0]=>
        array(3) {
          ["OPTION_ID"]=>
          int(1)
          ["OPTION_VALUE"]=>
          string(11) "Sarah "
          ["OPTION_DEFAULT"]=>
          bool(false)
        }
      }
      ["DEPENDENCY"]=>
      NULL
      ["JOIN_OBJECT"]=>
      NULL
    }
1
  • Instead of var_dump, can you provide your php array as var_export instead? Commented Jan 31, 2022 at 23:54

1 Answer 1

1

If I understand you correctly, it looks like array_filter will help you here...

<?php

$getAllCustomerCustomFields = array_filter($getAllCustomerCustomFields, function($key) {
    return $key == 'Secondary_Volunteering__c' || $key == 'Referral_Source_within_CF__c';
}, ARRAY_FILTER_USE_KEY)

This basically filters off (reduces) all the unwanted keys of the array

Sign up to request clarification or add additional context in comments.

3 Comments

Ooo, nice. I'll give it a try. Thanks.
Perfect, I can now see how that works here. Thank you so much!
You're welcome. Happy coding. Please don't forget to accept the answer if you are happy with it.... it gives me great satisfaction! *edit - I see you have done so - thank you!

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.