Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to search and remove sub arrays from array if string not found in list

I’m stuck. 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
    }

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading