gpt4 book ai didi

php - 多维数组,去掉key和value匹配到另一个数组的数组

转载 作者:可可西里 更新时间:2023-11-01 13:19:13 25 4
gpt4 key购买 nike

我想删除 measurement 或 altunit 与另一个数组匹配的重复项,但如果它们为空则忽略。

Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)

[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)

[2] => Array
(
[id] => 4
[altunit] => %
[measurement] =>
)

[3] => Array
(
[id] => 5
[altunit] =>
[measurement] => 6
)

[4] => Array
(
[id] => 6
[altunit] =>
[measurement] => 6
)

)

成为

Array
(
[0] => Array
(
[id] => 2
[altunit] => %
[measurement] =>
)

[1] => Array
(
[id] => 3
[altunit] =>
[measurement] => 6
)

)

我能想到的最好的是:

$test = array ( 0 => array ( 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ), 1 => array ( 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ), 2 => array ( 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ), 3 => array ( 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ), 4 => array ( 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ), );

$num = [];
foreach($test as $k => $v) $num[] = $v['measurement'];

但这只适用于测量,并删除了 id 和 altunit 键。

最佳答案

嗯,为测量和 altunit 制作一个“已知值”数组,然后检查它是否存在于其余值上。

类似于:

$knowed_altunit=array();
$knowed_measurement=array();

foreach($test as $k=>$v){
if(in_array($v['altunit'],$knowed_altunit)
|| in_array($v['mesurement'],$knowed_measurement)){
//if the value of altunit or measurement is already knowed then remove the entry from the array,
unset($test[$k]);
}else{
//if it never been seen, add it so further entry can be checked agaisnt the knowed value
$knowed_altunit[]=$v['altunit'];
$knowed_measurement[]=$v['mesurement'];
}
}

抱歉,如果有任何打字错误,但可能会帮助您全神贯注地解决问题。

关于php - 多维数组,去掉key和value匹配到另一个数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com