如何删除具有乘法数组值的元素?

how to remove elements with multiplication array value?

我有一个乘法关联数组,其中有四个值。下面我有数组:

Array
(
    [0] => Array
        (
            [name] => Morning Ride
            [distance] => 1723.3
            [type] => Ride
            [id] => 2011096935
        )

[1] => Array
    (
        [name] => Evening Walk
        [distance] => 3165.5
        [type] => Walk
        [id] => 2008414015
    )

[2] => Array
    (
        [name] => Morning walk
        [distance] => 2262.9
        [type] => Walk
        [id] => 1963423515
    )

[3] => Array
    (
        [name] => Evening Runining
        [distance] => 531.2
        [type] => Run
        [id] => 1951087309
    )
)

数组中有值"type",其中一个是Ride,一个是运行,两个是Walk。现在我担心的是我只想要数组中的 Walk 类型值而不是所有值。那么我该怎么做呢。

我为此使用了一个函数:

function removeElementWithValue($array, $key, $value){
     foreach($array as $subKey => $subArray){
          if($subArray[$key] == $value){
               unset($array[$subKey]);
          }
     }
     return $array;
}
$activities = removeElementWithValue($stravaactvity, "type", 'Run');

这只会删除我的 运行 类型值,而不是 Ride one。

只需 运行 您的函数以及您的其他值。

function removeElementWithValue($array, $key, $value){
     foreach($array as $subKey => $subArray){
          if($subArray[$key] == $value){
               unset($array[$subKey]);
          }
     }
     return $array;
}
$activities = removeElementWithValue($stravaactvity, "type", 'Run');
$activities = removeElementWithValue($activities, "type", 'Ride');

如果您只想保留 Walk,您也可以使用 array_filter 而不是删除其他的。

$res = array_filter($arrays, function($x) {
    return $x["type"] === "Walk";
});

print_r($res);

看到一个php demo