array-filter

How to filter multi-dimensional array (with more than two levels) by value?

妖精的绣舞 提交于 2021-02-10 08:34:30
问题 Assuming the following three-dimensional array is given: Array ( [0] => Array ( [0] => Array ( [foo] => bar ) [1] => Array ( [foo] => bar2 ) [2] => Array ( [foo] => bar3 ) ) [1] => Array ( [0] => Array ( [foo] => baz ) [1] => Array ( [foo] => bar ) [2] => Array ( [foo] => bar5 ) ) ) Now I need to filter it. If [foo] is not equal to bar the corresponding inner array should get removed. The result would be: Array ( [0] => Array ( [0] => Array ( [foo] => bar ) ) [1] => Array ( [1] => Array (

How to filter multi-dimensional array (with more than two levels) by value?

旧街凉风 提交于 2021-02-10 08:34:00
问题 Assuming the following three-dimensional array is given: Array ( [0] => Array ( [0] => Array ( [foo] => bar ) [1] => Array ( [foo] => bar2 ) [2] => Array ( [foo] => bar3 ) ) [1] => Array ( [0] => Array ( [foo] => baz ) [1] => Array ( [foo] => bar ) [2] => Array ( [foo] => bar5 ) ) ) Now I need to filter it. If [foo] is not equal to bar the corresponding inner array should get removed. The result would be: Array ( [0] => Array ( [0] => Array ( [foo] => bar ) ) [1] => Array ( [1] => Array (

First element of array by condition [duplicate]

北战南征 提交于 2020-03-22 11:06:32
问题 This question already has answers here : Elegant way to search an PHP array using a user-defined function (6 answers) Closed last year . I am looking for an elegant way to get the first (and only the first) element of an array that satisfies a given condition. Simple example: Input: [ ['value' => 100, 'tag' => 'a'], ['value' => 200, 'tag' => 'b'], ['value' => 300, 'tag' => 'a'], ] Condition: $element['value'] > 199 Expected output: ['value' => 200, 'tag' => 'b'] I came up with several

First element of array by condition [duplicate]

半世苍凉 提交于 2020-03-22 11:05:30
问题 This question already has answers here : Elegant way to search an PHP array using a user-defined function (6 answers) Closed last year . I am looking for an elegant way to get the first (and only the first) element of an array that satisfies a given condition. Simple example: Input: [ ['value' => 100, 'tag' => 'a'], ['value' => 200, 'tag' => 'b'], ['value' => 300, 'tag' => 'a'], ] Condition: $element['value'] > 199 Expected output: ['value' => 200, 'tag' => 'b'] I came up with several

array_filter doesn't seems to work for words having apostrophe and dash

拥有回忆 提交于 2020-03-10 05:14:11
问题 The bounty expires in 7 days . Answers to this question are eligible for a +50 reputation bounty. flash wants to draw more attention to this question: The answer which I have tried works fine here 3v4l.org/FOjXu but its not working on my machine. I have a php code as shown below: $scheduled_streams = \CTIME\LiveStreams\get_live_today_streams(); // Line A echo '<pre>'; print_r($scheduled_streams); echo '</pre>'; // Line B echo '<pre>'; var_dump($scheduled_streams); echo '</pre>'; // Line C

Multiple array_filter and strpos

北城以北 提交于 2020-01-30 11:40:09
问题 I want to return array that does not contains a list of characters. Below code works fine for one keyword ( 'bc' ). $array = array("abc", "def", "ghi"); $filterArray = array_filter($array, function ($var) {return(strpos($var, 'bc') === false);}); print_r($filterArray); However, below code does not work when I try to filter out multiple keywords by using $excludeKeyword_arr and foreach . $array = array("abc", "def", "ghi"); $excludeKeyword_arr = ("ab", "de"); foreach($excludeKeyword_arr as

Array_filter in the context of an object, with private callback

对着背影说爱祢 提交于 2020-01-01 07:43:26
问题 I want to filter an array, using the array_filter function. It hints at using call_user_func under water, but does not mention anything about how to use within the context of a class/object. Some pseudocode to explain my goal: class RelatedSearchBlock { //... private function get_filtered_docs() { return array_filter($this->get_docs(), 'filter_item'); } private filter_item() { return ($doc->somevalue == 123) } } Would I need to change 'filter_item' into array($this, 'filter_item') ? Is what I

Array_filter in the context of an object, with private callback

元气小坏坏 提交于 2020-01-01 07:42:05
问题 I want to filter an array, using the array_filter function. It hints at using call_user_func under water, but does not mention anything about how to use within the context of a class/object. Some pseudocode to explain my goal: class RelatedSearchBlock { //... private function get_filtered_docs() { return array_filter($this->get_docs(), 'filter_item'); } private filter_item() { return ($doc->somevalue == 123) } } Would I need to change 'filter_item' into array($this, 'filter_item') ? Is what I

How to use array_filter with callback in php 5.2 [duplicate]

流过昼夜 提交于 2019-12-31 05:31:08
问题 This question already has answers here : PHP parse/syntax errors; and how to solve them (17 answers) Closed 3 years ago . I am trying to use array_filter with call back in php 5.2, but I get the following error: Parse error: syntax error, unexpected T_FUNCTION And I did search the solution using the error in Google search and found that Php 5.2 does not support callback . The code I am working on is: $result = array_filter($lines, function($line) { return stripos($line,"ID:")!==false; }); How

Explain how this custom function works PHP

随声附和 提交于 2019-12-19 17:46:38
问题 Here this function in PHP that allows to merge any N amount of different length arrays in a fashion that output array will be in next order: Array1[0],Array2[0],..,ArrayN[0],Array1[1],Array2[1],..,ArrayN[1]... : function array_zip_merge() { $output = array(); // The loop incrementer takes each array out of the loop as it gets emptied by array_shift(). for ($args = func_get_args(); count($args); $args = array_filter($args)) { // &$arg allows array_shift() to change the original. foreach ($args