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

Time:02-01

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.

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
    }

CodePudding user response:

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

  •  Tags:  
  • Related