array-filter

PHP - How to remove empty entries of an array recursively?

試著忘記壹切 提交于 2019-12-18 12:59:13
问题 I need to remove empty entries on multilevel arrays. For now I can remove entries with empty sub-arrays, but not empty arrays... confused, so do I... I think the code will help to explain better... <?php /** * * This function remove empty entries on arrays * @param array $array */ function removeEmptysFromArray($array) { $filtered = array_filter($array, 'removeEmptyItems'); return $filtered; } /** * * This is a Callback function to use in array_filter() * @param array $item */ function

Array_filter and empty()

佐手、 提交于 2019-12-18 06:57:09
问题 Warning: array_filter() expects parameter 2 to be a valid callback, function 'empty' not found or invalid function name.... Why is empty considered a invalid callback? $arr = array_filter($arr, 'empty'); This works: if(empty($arr['foo'])) die(); 回答1: Answer empty() is not a function but a language construct and array_filter() can only accept a function as its callback. This is given as a small note on the manual page: Note: Because this is a language construct and not a function, it cannot be

Remove NULL, FALSE, and '' - but not 0 - from a PHP array

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 18:25:47
问题 I want to remove NULL , FALSE and '' values . I used array_filter but it removes the 0 ' s also. Is there any function to do what I want? array(NULL,FALSE,'',0,1) -> array(0,1) 回答1: array_filter should work fine if you use the identical comparison operator. here's an example $values = [NULL, FALSE, '', 0, 1]; function myFilter($var){ return ($var !== NULL && $var !== FALSE && $var !== ''); } $res = array_filter($values, 'myFilter'); Or if you don't want to define a filtering function, you can

filter array by string and separate in to 2 arrays

二次信任 提交于 2019-12-13 18:13:33
问题 I have a array like this Array ( [operator_15] => 3 [fiter_15] => 4 [operator_17] => 5 [fiter_17] => 5 [operator_19] => 4 [fiter_19] => 2 ) I want to separate this array in to 2 arrays: key starting from fiter_ key starting from operator_ I used array filter and it doesn't work. any other option? $array = array_filter($fitered_values, function($key) { return strpos($key, 'fiter_') === 0; }); 回答1: Give a try with below and see if its solve your problem $array = array ( 'operator_15' => 3,

Use NSPredicate to filter by object attribute

£可爱£侵袭症+ 提交于 2019-12-13 14:01:34
问题 I have a mutable array of custom objects. I want to filter that array by attribute of the object, for example myObject.attributeOne . How can I create the NSPredicate to use with [myArrayOfObjects filterUsingPredicate:<the_predicate>] 回答1: Use it in this way: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FriendStatus == 1"]; NSMutableArray *filtered = [MessageArray filteredArrayUsingPredicate:predicate]; 来源: https://stackoverflow.com/questions/3386079/use-nspredicate-to-filter

filter array by key

可紊 提交于 2019-12-13 07:11:18
问题 I have this little function to filter my array by keys: private function filterMyArray( ) { function check( $v ) { return $v['type'] == 'video'; } return array_filter( $array, 'check' ); } This works great but since I have more keys to filter, I was thinking in a way to pass a variable from the main function: filterMyArray($key_to_serch) without success, also I've tried a global variable, but seems not work. Due some confusion in my question :), I need something like this: private function

Filter ArrayObject (PHP)

亡梦爱人 提交于 2019-12-12 22:14:54
问题 I have my data in ArrayObject , simply representing an array. I need to filter the data, function array_filter() would work great. However, it does not work with ArrayObject as argument. What's the best way to treat with this? Is there any standard function which handles filtering for me? Example: $my_data = ArrayObject(array(1,2,3)); $result = array_object_filter($my_data, function($item) { return $item !== 2; }); Is there any array_object_filter function? 回答1: How about you export it to an

ArrayFilter in mongoose

我们两清 提交于 2019-12-12 13:33:14
问题 Convert the query into node. In which version arrayfilter work in mongoose or how can run these in node app db.getCollection('student').update( { "_id": ObjectId('5a377d62d21a3025a3c3aa49') }, { $set: { "examples.$[i].isDeleted": true, "examples.$[i].updatedAt": new Date(), "updatedAt": new Date() } }, { arrayFilters: [ { "i._id": { $in:[ ObjectId("5a377d62d21a3025a3c3aa4d"), ObjectId("5a377d62d21a3025a3c3aa4c") ] } } ], multi: true } ) https://docs.mongodb.com/manual/reference/operator

Filter one array with another

痞子三分冷 提交于 2019-12-11 14:59:09
问题 I know this been answered multiple times but I can't seem to find an answer based on my situation. I have two arrays: words ['word1', 'word2', 'word3'] texts [ {name: 'blah', description: 'word4'}, {name: 'blah2', description: 'word1'}, {name: 'blah3', description: 'word5'} ] I am trying to filter the two arrays and return true if there is a match I have seen several examples of simple arrays of numbers but that does not apply here. 回答1: You can iterate texts using Array.prototype.some() and

How to filter array values from another arrays values and return new array?

冷暖自知 提交于 2019-12-10 14:48:51
问题 I have two arrays: $all_languages and $taken_languages . One contains all languages (like 200 or something), but second - languages that have been chosen before (from 0 to 200). I need to remove all languages that have been taken ( $taken_languages ) from $all_languages and return new array - $available_languages . My solution was two loops, but, first, it doesn't work as expected, second - it's 'not cool' and I believe that there are better solutions! Can you point me to the correct path?