Answers for "php array filter syntax"

PHP
14

php array filter syntax

$numbers = [2, 4, 6, 8, 10];

function MyFunction($number)
{
  return $number > 5;
}

$filteredArray = array_filter($numbers, "MyFunction");

/**
 * `$filteredArray` now contains: `[6, 8, 10]`
 * NB: Use this to remove what you don't want in the array
 * @see `array_map` when you want to alter/change elements
 * in the array.
 */
Posted by: Guest on February-21-2020
-1

array_filter php

$var = [
  'first' => 'one',
  'second' => null,
  'third' => 'three',
];


$filteredArray = array_filter($var);
// output: ['first'=>'one,'third'=>'three']
Posted by: Guest on May-03-2021

Browse Popular Code Answers by Language