Answers for "array_filter()"

PHP
6

array filter use key

$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed  = ['foo', 'bar'];
$filtered = array_filter(
    $my_array,
    function ($key) use ($allowed) {
        return in_array($key, $allowed);
    },
    ARRAY_FILTER_USE_KEY
);
Posted by: Guest on November-04-2020
1

php array_filter

$array = [1, 2, 3, 4, 5];

$filtered = array_filter($array, function($item) {
    return $item != 4; // Return (include) current item if expression is truthy
});

// $filtered = [1, 2, 3, 5]
Posted by: Guest on June-03-2021
-1

array_filter php

array_filter example
Posted by: Guest on June-08-2021
-2

filter value in array php return single value

?php
$data= [
    0 => [1, 'test1'],
    1 => [2, 'test2'],
    2 => [3, 'test3'],
];

$ids = array_map(function($item) {
    return $item[0];
}, $data);

var_dump($ids);
Posted by: Guest on July-22-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