嵌套数组上的PHP/Laravel数组差异-删除假值
我有两个数组,其中也包含嵌套数组,我想将它们相互比较。如果第一个数组中的值为嵌套数组上的PHP/Laravel数组差异-删除假值,php,arrays,laravel,Php,Arrays,Laravel,我有两个数组,其中也包含嵌套数组,我想将它们相互比较。如果第一个数组中的值为false,我希望从第二个数组中删除该键 第一个数组:$arrayOne: “实体”=>[ 'number'=>false, '项目'=>[ [ 'name'=>false, ], ], “总计”=>[ “gross”=>正确, “税”=>[ “金额”=>true, ], ] ]; 然后,我有第二个数组$arrayTwo: “实体”=>[ “编号”=>“8000000”, '项目'=>[ [ “名称”=>“螺栓切割机”
false
,我希望从第二个数组中删除该键
第一个数组:$arrayOne
:
“实体”=>[
'number'=>false,
'项目'=>[
[
'name'=>false,
],
],
“总计”=>[
“gross”=>正确,
“税”=>[
“金额”=>true,
],
]
];
然后,我有第二个数组$arrayTwo
:
“实体”=>[
“编号”=>“8000000”,
'项目'=>[
[
“名称”=>“螺栓切割机”,
],
[
'name'=>'Wire cutter',
],
],
“总计”=>[
“总量”=>120.52,
“税”=>[
“金额”=>90.21,
],
]
];
正如您在第一个数组中看到的,键:number
和items.name
是false
。这两个键不应位于最终数组中,因此使其看起来如下所示:
“实体”=>[
'项目'=>[],
“总计”=>[
“总量”=>120.52,
“税”=>[],
]
];
如何使用嵌套数组实现这一点?我已成功地使用以下方法为阵列的“第一级”执行此操作:
foreach($arrayOne作为$key=>$entity){
如果($entity==false){
未设置($arrayTwo[$key]);
}
}
但是,如前所述,这只处理数组的第一级,而不处理嵌套数组。我已经测试过了,它可以正常工作
use Illuminate\Support\Arr;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// comparing to removed number key version of second array
unset($flattened_array2[$key2]);
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
嵌套数组时支持空数组的新更新
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
// Function to check string starting
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$removerCounter[$key] = 0;
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
// comparing to removed number key version of second array
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// check if it contain filtered number key then
if(preg_replace('/[0-9]+\./', '', $key2) != $key2){
$currentKey = $key2;
preg_match_all("/[0-9]+\./", $currentKey, $hits);
if(!isset($hits[0]) || empty($hits[0]) ){
break;
}
$needle = $hits[0] [ count($hits[0]) - 1 ];
// it is inside key not at the begining
if(stripos($currentKey, $needle) != 0){
// if parent key has no child so let it be empty array
$parentKey = trim(substr($currentKey, 0, stripos($currentKey, $needle)), '.');
// search inside secound array for parentKey of this nested array
$searchAndFound = false;
foreach ($flattened_array2 as $inner_key2 => $inner_value2) {
if( !($searchAndFound) && $inner_key2 != $key2 && Str::startsWith($inner_key2, $parentKey) ) {
$searchAndFound = true;
}
}
if( $searchAndFound ){
// die("e");
unset($flattened_array2[$key2]);
$removerCounter[$key]++;
//remove this key and go for next key in secound array
continue;
}
if($removerCounter[$key] > 0){
unset($flattened_array2[$key2]);
}
$flattened_array2[$parentKey] = [];
}
}
else{
unset($flattened_array2[$key2]);
}
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
人们对它的期望更为不满。但是有诀窍吗:s.我确实测试过这个,它会很好地工作
use Illuminate\Support\Arr;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// comparing to removed number key version of second array
unset($flattened_array2[$key2]);
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
嵌套数组时支持空数组的新更新
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
// Function to check string starting
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$removerCounter[$key] = 0;
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
// comparing to removed number key version of second array
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// check if it contain filtered number key then
if(preg_replace('/[0-9]+\./', '', $key2) != $key2){
$currentKey = $key2;
preg_match_all("/[0-9]+\./", $currentKey, $hits);
if(!isset($hits[0]) || empty($hits[0]) ){
break;
}
$needle = $hits[0] [ count($hits[0]) - 1 ];
// it is inside key not at the begining
if(stripos($currentKey, $needle) != 0){
// if parent key has no child so let it be empty array
$parentKey = trim(substr($currentKey, 0, stripos($currentKey, $needle)), '.');
// search inside secound array for parentKey of this nested array
$searchAndFound = false;
foreach ($flattened_array2 as $inner_key2 => $inner_value2) {
if( !($searchAndFound) && $inner_key2 != $key2 && Str::startsWith($inner_key2, $parentKey) ) {
$searchAndFound = true;
}
}
if( $searchAndFound ){
// die("e");
unset($flattened_array2[$key2]);
$removerCounter[$key]++;
//remove this key and go for next key in secound array
continue;
}
if($removerCounter[$key] > 0){
unset($flattened_array2[$key2]);
}
$flattened_array2[$parentKey] = [];
}
}
else{
unset($flattened_array2[$key2]);
}
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
人们对它的期望更为不满。但是诀窍是:s.你能发布你想要比较的最终数组的结构吗?他们是用拉雷维尔雄辩还是拉雷维尔学院?我想知道是否有任何底层方法可用于此操作。最终数组在我的OP中共享。它们由Laravel集合检索:)我的意思是,您的第二个数组是否可能包含多个实体?如果这和你说的一模一样,其中一个答案应该有效。但是,如果在第二个数组中有多个实体,并且每个实体都需要过滤,这是另一个问题。我试着按照你的要求回答第一个问题。第二种方法消除了税数组,但这是可以修复的。所以这就是为什么我想看看问题出在哪里。@ElieMorin啊,我明白了!是的,第二个(通常是嵌套数组)可以包含多个实体-我想这就是第一个答案不起作用的原因(如果它不包含多个实体,它确实起作用)好吧,在这种情况下,您应该编辑您的问题,以便我们能够帮助您正确地发布您尝试比较的最终数组的结构,可以吗?他们是用拉雷维尔雄辩还是拉雷维尔学院?我想知道是否有任何底层方法可用于此操作。最终数组在我的OP中共享。它们由Laravel集合检索:)我的意思是,您的第二个数组是否可能包含多个实体?如果这和你说的一模一样,其中一个答案应该有效。但是,如果在第二个数组中有多个实体,并且每个实体都需要过滤,这是另一个问题。我试着按照你的要求回答第一个问题。第二种方法消除了税数组,但这是可以修复的。所以这就是为什么我想看看问题出在哪里。@ElieMorin啊,我明白了!是的,第二个(通常是嵌套数组)可以包含多个实体-我想这就是第一个答案不起作用的原因(如果它不包含多个实体,它就起作用)好吧,在这种情况下,你应该编辑你的问题,这样我们就可以帮助你了。如果嵌套数组有多个项,这就不起作用了。点符号将是:
items.0.name
和items.1.name
等。当然不能,您在问题中没有提到这一点。让我看看。我道歉!这不是我原来的问题。谢谢你帮助我!如果entities.items.name
设置为true,它会删除它吗?@ElieMorin否,问题是只删除第一个数组中设置为false的项。所以我检查了entity value if is false,然后我尝试在第二个数组中查找与该值相关的键。如果嵌套数组有多个项,则这不起作用。点符号将是:items.0.name
和items.1.name
等。当然不能,您在问题中没有提到这一点。让我看看。我道歉!这不是我原来的问题。谢谢你帮助我!如果entities.items.name
设置为true,它会删除它吗?@ElieMorin否,问题是只删除第一个数组中设置为false的项。所以我检查了entity value,如果它为false,那么我尝试在第二个数组中找到与该值相关的键。