基于另一个数组通过键对数组进行相交和排序?
Intersect and Sort an Array by keys based on another Array?
类似于问题:Sort an Array by keys based on another Array?只是我想删除所有不常见的密钥。
基本上我尝试通过 array_intersect_key($VARIABLES, array_flip($signature_args));
and then tried to sort it with array_merge(array_flip($signature_args), $filtered)
通过键过滤变量
此处显示:
$VARIABLES = array('3'=>'4', '4'=>'5', '1'=>'2');
$signature_args = array('1', '2', '3');
$filtered = array_intersect_key($VARIABLES, array_flip($signature_args));
var_dump($filtered);
var_dump(array_merge(array_flip($signature_args), $filtered));
产生:
array(2) {
[3]=>
string(1) "4"
[1]=>
string(1) "2"
}
array(5) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
[3]=>
string(1) "4"
[4]=>
string(1) "2"
}
而不是
array(2) {
[3]=>
string(1) "4"
[1]=>
string(1) "2"
}
array(2) {
[1]=>
string(1) "2"
[3]=>
string(1) "4"
}
如我所料,为什么?
这应该适合你:
<?php
$VARIABLES = array('3'=>'4', '4'=>'5', '1'=>'2');
$signature_args = array('2', '3', '1');
$filtered = array_intersect_key($VARIABLES, array_flip($signature_args));
var_dump($filtered);
$ordered = array();
foreach ($signature_args as $key) {
if(!empty($filtered[$key]))
$ordered[$key] = $filtered[$key] ;
}
var_dump($ordered);
?>
或者如果你愿意,你可以使用这个:
array_walk($signature_args, function($key) {
if(!empty($filtered[$key])) $ordered[$key] = $filtered[$key] ;
}, $ordered);
要使用“允许”数组不仅过滤输入数组而且对输出进行排序,将两个数组相互过滤,然后将输入数组替换(或合并)到允许数组中。
代码:(Demo)
$input = ['3' => '4', '4' => '5', '1' => '2'];
$allowed = ['1', '2', '3'];
$flipAllowed = array_flip($allowed);
var_export(
array_replace(
array_intersect_key($flipAllowed, $input), // filtered allowed
array_intersect_key($input, $flipAllowed) // filtered input
)
);
输出:
array (
1 => '2',
3 => '4',
)
类似于问题:Sort an Array by keys based on another Array?只是我想删除所有不常见的密钥。
基本上我尝试通过 array_intersect_key($VARIABLES, array_flip($signature_args));
and then tried to sort it with array_merge(array_flip($signature_args), $filtered)
此处显示:
$VARIABLES = array('3'=>'4', '4'=>'5', '1'=>'2');
$signature_args = array('1', '2', '3');
$filtered = array_intersect_key($VARIABLES, array_flip($signature_args));
var_dump($filtered);
var_dump(array_merge(array_flip($signature_args), $filtered));
产生:
array(2) {
[3]=>
string(1) "4"
[1]=>
string(1) "2"
}
array(5) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
[3]=>
string(1) "4"
[4]=>
string(1) "2"
}
而不是
array(2) {
[3]=>
string(1) "4"
[1]=>
string(1) "2"
}
array(2) {
[1]=>
string(1) "2"
[3]=>
string(1) "4"
}
如我所料,为什么?
这应该适合你:
<?php
$VARIABLES = array('3'=>'4', '4'=>'5', '1'=>'2');
$signature_args = array('2', '3', '1');
$filtered = array_intersect_key($VARIABLES, array_flip($signature_args));
var_dump($filtered);
$ordered = array();
foreach ($signature_args as $key) {
if(!empty($filtered[$key]))
$ordered[$key] = $filtered[$key] ;
}
var_dump($ordered);
?>
或者如果你愿意,你可以使用这个:
array_walk($signature_args, function($key) {
if(!empty($filtered[$key])) $ordered[$key] = $filtered[$key] ;
}, $ordered);
要使用“允许”数组不仅过滤输入数组而且对输出进行排序,将两个数组相互过滤,然后将输入数组替换(或合并)到允许数组中。
代码:(Demo)
$input = ['3' => '4', '4' => '5', '1' => '2'];
$allowed = ['1', '2', '3'];
$flipAllowed = array_flip($allowed);
var_export(
array_replace(
array_intersect_key($flipAllowed, $input), // filtered allowed
array_intersect_key($input, $flipAllowed) // filtered input
)
);
输出:
array (
1 => '2',
3 => '4',
)