Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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_Multidimensional Array - Fatal编程技术网

PHP从多维数组中删除特定数组

PHP从多维数组中删除特定数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我在PHP中有一个多维数组,需要根据其中一个数组中某个项的值删除一个数组: 示例数组 array( "0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"), "1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"), "2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00") ) 我知道我想删

我在PHP中有一个多维数组,需要根据其中一个数组中某个项的值删除一个数组:

示例数组

array(
   "0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"),
   "1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
   "2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
)
我知道我想删除键
0
中包含
joe
的数组,但我只想删除键
1
中包含最新日期的
joe
的数组。以下是我试图完成的输出:

array(
   "0"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
   "1"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
) 

在PHP中,除了循环每个数组外,还有什么简单的方法可以做到这一点吗?

这里有一个非循环方法,它使用array\u intersect和array\u列查找“Joe”,然后删除最大array\u键,因为我第一次按日期排序数组

usort($arr, function($a, $b) {
    return $a[1] <=> $b[1];
}); // This returns the array sorted by date

// Array_column grabs all the names in the array to a single array.
// Array_intersect matches it to the name "Joe" and returns the names and keys of "Joe"
$joes = array_intersect(array_column($arr, 0), ["joe"]);

// Array_keys grabs the keys from the array as values
// Max finds the maximum value (key)
$current = max(array_keys($joes));
unset($arr[$current]);

var_dump($arr);
usort($arr,function($a,$b){
返回$a[1]$b[1];
}); // 这将返回按日期排序的数组
//Array_column将数组中的所有名称捕获到单个数组中。
//数组_intersect将其与名称“Joe”匹配,并返回“Joe”的名称和键
$joes=array\u intersect(array\u列($arr,0),[“joe”]);
//Array_keys从数组中获取键作为值
//Max查找最大值(键)
$current=max(数组_键($joes));
未结算($arr[$current]);
var_转储($arr);

如果要重置数组中的键,请编辑忘记添加数组_值()


只需添加
$arr=array\u值($arr)在unset之后。

这里有一个非循环方法,它使用array\u intersect和array\u列查找“Joe”,然后删除最大array\u键,因为我第一次按日期对数组排序

usort($arr, function($a, $b) {
    return $a[1] <=> $b[1];
}); // This returns the array sorted by date

// Array_column grabs all the names in the array to a single array.
// Array_intersect matches it to the name "Joe" and returns the names and keys of "Joe"
$joes = array_intersect(array_column($arr, 0), ["joe"]);

// Array_keys grabs the keys from the array as values
// Max finds the maximum value (key)
$current = max(array_keys($joes));
unset($arr[$current]);

var_dump($arr);
usort($arr,function($a,$b){
返回$a[1]$b[1];
}); // 这将返回按日期排序的数组
//Array_column将数组中的所有名称捕获到单个数组中。
//数组_intersect将其与名称“Joe”匹配,并返回“Joe”的名称和键
$joes=array\u intersect(array\u列($arr,0),[“joe”]);
//Array_keys从数组中获取键作为值
//Max查找最大值(键)
$current=max(数组_键($joes));
未结算($arr[$current]);
var_转储($arr);

如果要重置数组中的键,请编辑忘记添加数组_值()


只需添加
$arr=array\u值($arr)在取消设置之后。

我会这样做:

<?php
 $foo = array(
   "0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"),
   "1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
   "2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
);


$tmp = [];  
foreach($foo as $k => $v) {
    if ($v[0] === 'joe') {
        $tmp[$v[1]] = $k;
    }
}
if (!empty($tmp)) {
    sort($tmp);  //think that is sane with date format?
    unset($foo[reset($tmp)]);
}

var_dump($foo);

我会这样做:

<?php
 $foo = array(
   "0"=>array("0"=>"joe", "1"=>"2018-07-18 09:00:00"),
   "1"=>array("0"=>"tom", "1"=>"2018-07-17 09:00:00"),
   "2"=>array("0"=>"joe", "1"=>"2018-07-14 09:00:00")
);


$tmp = [];  
foreach($foo as $k => $v) {
    if ($v[0] === 'joe') {
        $tmp[$v[1]] = $k;
    }
}
if (!empty($tmp)) {
    sort($tmp);  //think that is sane with date format?
    unset($foo[reset($tmp)]);
}

var_dump($foo);

可能与否重复。我想是回叫。。。阵列滤波器?但是仍然是“循环”。最新的总是在顶部吗?最新的不一定总是在顶部。你的问题写得很混乱。将你的问题概括为“保留最老的乔”是否正确?如果有3个joe或两个joe具有相同的时间戳,您希望发生什么?如果有2个Tom和3个Joe会发生什么?可能是No的重复。我猜是回调。。。阵列滤波器?但是仍然是“循环”。最新的总是在顶部吗?最新的不一定总是在顶部。你的问题写得很混乱。将你的问题概括为“保留最老的乔”是否正确?如果有3个joe或两个joe具有相同的时间戳,您希望发生什么?如果有两个汤姆和三个乔会怎么样?很好。那个
array\u列
是一个很好的补充。@ficuscr同意,我在这里写的几乎每一个答案都使用array\u列。不过,我认为在backgroundarray_列中会有一些循环。不确定,很好。那个
array\u列
是一个很好的补充。@ficuscr同意,我在这里写的几乎每一个答案都使用array\u列。不过,我认为在backgroundarray_列中会有一些循环。不过我不确定。