“php array_filter” Kode Jawaban

php array_filter

$array = [
    ['name' => 'John', 'age' => 45],
    ['name' => 'Haley', 'age' => 42],
    ['name' => 'Ally', 'age' => 8],
    ['name' => 'Meylyn', 'age' => 5],
    ['name' => 'Nicholas', 'age' => 1],
];


$adults = array_filter($array, function($value) {
    return $value['age'] > 18;
});

foreach ($adults as $adult) {
    echo $adult['name'] . "<br/>";
}
Condemned Caracal

Filter Array Gunakan kunci

$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
);
Yoshkinawa

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]
DEVWAX

filter array php


<?php

$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];

var_dump(array_filter($arr, function($k) {
    return $k == 'b';
}, ARRAY_FILTER_USE_KEY));

var_dump(array_filter($arr, function($v, $k) {
    return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
?>

Dizzy Donkey

array filter php

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

function isPositive($number)
{
  return $number > 0;
}

$filteredArray = array_filter($numbers, "isPositive");
Coder Cuttlefish

nilai filter dalam array php mengembalikan nilai tunggal

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

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

var_dump($ids);
Lokesh003Coding

Jawaban yang mirip dengan “php array_filter”

Pertanyaan yang mirip dengan “php array_filter”

Lebih banyak jawaban terkait untuk “php array_filter” di PHP

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya