PHP: Filters elements of an array using a callback functionThe array_filter() function passes each value of a given array to a user defined function. If the user defined function allows, the current value from the array is returned into the result array. Show Note: The function Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from the array is returned into the result array. Array keys are preserved. Version: (PHP 4 and above) Syntax: array_filter(input_array, user_defined_function) Parameter:
Return value: The filtered array. Value Type: Array Example:
Output: Array ( [Item2] => 200 [Item3] => 125 ) Pictorial Presentation: View the example in the browser Practice here online : See alsoPHP Function Reference Previous:array_fill PHP: Tips of the DayPHP: How to push both value and key into PHP array? No, there is no array_push() equivalent for associative arrays because there is no way determine the next key. You'll have to use $arrayname[indexname] = $value; Ref : https://bit.ly/3cp9Gij
Php array_filter without key preservationThe filtered version of [0] => 'foo' [1] => null [2] => 'bar' is [0] => 'foo' [2] => 'bar' [0] => 'foo' [1] => 'bar' $array=['foo',NULL,'bar',0,false,null,'0','']; var_export(array_values(array_filter($array))); // NOT GOOD!!!!! array ( 0 => 'foo', 1 => 'bar', ) var_export(array_values(array_filter($array,function($v){return !is_null($v);}))); // good foreach($array as $v){ if($v!==null){$result[]=$v;} } var_export($result); // good array_walk($array,function($v)use(&$filtered){if(!is_null($v)){$filtered[]=$v;}}); var_export($filtered); // good array ( 0 => 'foo', 1 => 'bar', 2 => 0, 3 => false, 4 => '0', 5 => '', ) $array = ['foo', NULL, 'bar', 0, false, null, '0', '']; $array = [...array_filter($array)]; var_export($array); array ( 0 => 'foo', 1 => 'bar', ) Array_filterOdd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 ) Array ( [0] => foo [2] => -1 ) array(1) { ["b"]=> int(2) } array(2) { ["b"]=> int(2) ["d"]=> int(4) } PHP | array_filter() Functionarray array_filter($array, $callback_function, $flag) Array ( [0] => 12 [1] => 0 [2] => 0 [3] => 18 [5] => 0 [6] => 46 ) Array ( [0] => 12 [3] => 18 [4] => 27 [6] => 46 ) PHP array_filter: How to Filter an Array in PHP<?php // app.php function even($value) { return $value % 2 == 0; } $arr = [1, 2, 3, 4, 5]; print_r(array_filter($arr,"even")); <?php // app.php $arr = [1, 2, 3, 4, 5]; $output = array_filter($arr, function($value) { return $value % 2 == 0; }); print_r($output); <?php // app.php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; $outputA = array_filter($arr, function($k) { return $k == 'b'; }, ARRAY_FILTER_USE_KEY); print_r($outputA); <?php // app.php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; $outputB = array_filter($arr, function($v, $k) { return $k == 'b' || $v == 4; }, ARRAY_FILTER_USE_BOTH); print_r($outputB); <?php // app.php $data = array('first' => 1, 'second' => 2, 'third' => 3); $data = array_filter($data, function ($item) use (&$data) { echo "Filtering key ", key($data)."\n"; next($data); }); Array_flipArray ( [oranges] => 0 [apples] => 1 [pears] => 2 ) Array ( [1] => b [2] => c ) How to Filter Multidimensional Array by Key Value in PHP$array = array( array('name' => 'John Doe', 'email' => '[email protected]'), array('name' => 'Marry Lies', 'email' => '[email protected]'), array('name' => 'Andrew Joe', 'email' => '[email protected]'),);$like = 'jo';$result = array_filter($array, function ($item) use ($like) { if (stripos($item['name'], $like) !== false) { return true; } return false;}); Array ( [0] => Array ( [name] => John Doe [email] => [email protected] ) [2] => Array ( [name] => Andrew Joe [email] => [email protected] ) ) PHP array_reverse.wp-block-code { border: 0; padding: 0; } .wp-block-code > div { overflow: auto; } .shcb-language { border: 0; clip: rect(1px, 1px, 1px, 1px); -webkit-clip-path: inset(50%); clip-path: inset(50%); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; word-wrap: normal; word-break: normal; } .hljs { box-sizing: border-box; } .hljs.shcb-code-table { display: table; width: 100%; } .hljs.shcb-code-table > .shcb-loc { color: inherit; display: table-row; width: 100%; } .hljs.shcb-code-table .shcb-loc > span { display: table-cell; } .wp-block-code code.hljs:not(.shcb-wrap-lines) { white-space: pre; } .wp-block-code code.hljs.shcb-wrap-lines { white-space: pre-wrap; } .hljs.shcb-line-numbers { border-spacing: 0; counter-reset: line; } .hljs.shcb-line-numbers > .shcb-loc { counter-increment: line; } .hljs.shcb-line-numbers .shcb-loc > span { padding-left: 0.75em; } .hljs.shcb-line-numbers .shcb-loc::before { border-right: 1px solid #ddd; content: counter(line); display: table-cell; padding: 0 0.75em; text-align: right; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; white-space: nowrap; width: 1%; }array_reverse ( array $array , bool $preserve_keys = false ) : arrayCode language: PHP (php) <?php $numbers = [10, 20, 30]; $reversed = array_reverse($numbers); print_r($reversed); print_r($numbers);Code language: HTML, XML (xml) Array ( [0] => 30 [1] => 20 [2] => 10 ) Array ( [0] => 10 [1] => 20 [2] => 30 )Code language: plaintext (plaintext) <?php $book = [ 'PHP Awesome', 999, ['Programming', 'Web development'], ]; $preserved = array_reverse($book, true); print_r($preserved);Code language: HTML, XML (xml) Array ( [2] => Array ( [0] => Programming [1] => Web development ) [1] => 999 [0] => PHP Awesome )Code language: PHP (php) Array_keysArray ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) Next Lesson PHP Tutorial |