Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何删除具有乘法数组值的元素?_Php_Arrays_Associative Array - Fatal编程技术网

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

Php 如何删除具有乘法数组值的元素?,php,arrays,associative-array,Php,Arrays,Associative Array,我有一个乘法关联数组,其中有四个值。以下是我的阵列: Array ( [0] => Array ( [name] => Morning Ride [distance] => 1723.3 [type] => Ride [id] => 2011096935 ) [1] => Array ( [name]

我有一个乘法关联数组,其中有四个值。以下是我的阵列:

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”,其中一个是乘坐,一个是跑步,两个是步行。现在我关心的是我只需要数组中的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');

这只会删除“我的跑步类型”值,而不会同时删除其中一个值。

只需使用其他值再次运行函数即可

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');

如果您只想保持
行走
,您也可以使用,而不是删除其他

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

print_r($res);

请参见a

您的答案完全符合我的要求。谢谢,伙计。另外,如果我需要距离类型大于2500的数组,我们还可以用距离过滤。