Search code examples
phparraysjsonforeachstrpos

Get Array in foreach loop from string


I have an Array of Strings, each String that exist in Advanced Custom Fields groups should be returned in foreach loop. Final results should be a single array of all values.

$lubuvna_groups = acf_get_field_groups();

$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);

foreach($ArrayDiffs as $ArrayDiff) { 
   $resultsFileToImports[] = $ArrayDiff;
}

//$keysToImports = implode(", ",$resultsFileToImports);
$keysToImports = 'group_lubuvna_contact, group_lubuvna_subscriber';
    foreach($resultsFileToImports as $resultsFileToImportsKey) { 
        $keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
                        
            return ( strpos($el['key'], $keysToImports) !== false );

        });
}

The above code returns only if one string exists in $keysToImports. Once there are more than one value it doesn't work. I am sure i am missing something, but can't find any solution in here! It shows me empty Array:

Array ( )

Maybe there is a different way how to get the arrays without strpos?

Final Array should looks like following:

Array ( [0] => Array ( [ID] => 0 [key] => group_lubuvna_contact [title] => ACF Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781382 [_valid] => 1 ) [1] => Array ( [ID] => 0 [key] => group_lubuvna_subscriber [title] => Lubuvna - Subscriber Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => post ) ) [1] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) [2] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => lubuvna_subscriber ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781369 [_valid] => 1 ) )

Solution

  • Try changing:

    $keysToImports = 'group_lubuvna_contact', 'group_lubuvna_subscriber';
    // then return
    return ( strpos($el['key'], $keysToImports) !== false );
    

    to

    $keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
    // then return
    return in_array($el['key'], $keysToImports);
    

    Full code update:

    
    $lubuvna_groups = acf_get_field_groups();
    
    $ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
    
    foreach($ArrayDiffs as $ArrayDiff) { 
       $resultsFileToImports[] = $ArrayDiff;
    }
    
    
    $keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
    foreach($resultsFileToImports as $resultsFileToImportsKey) { 
        $keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
            return in_array($el['key'], $keysToImports);
        });
    }