3 回答
TA贡献2021条经验 获得超8个赞
您可以在 PHP 中的一行中执行此操作,array_flip用于交换第二个数组的键和值,然后array_intersect_key在匹配的键上合并两个数组:
$colldata=array("bench-press-rod"=>'',"adidas-classic-backpack"=>'93549559913',"adidas-classic-backpack-legend-ink-multicolour"=>'',"puma-suede-classic-regal"=>'93549920361,93549723753');
$colldata2=array(0 => 'bench-press-rod',1 => 'adidas-classic-backpack');
print_r(array_intersect_key($colldata, array_flip($colldata2)));
输出:
Array
(
[bench-press-rod] =>
[adidas-classic-backpack] => 93549559913
)
TA贡献1784条经验 获得超2个赞
我能想到的最简单的方法是遍历第二个数组并将第一个数组中的匹配键添加到输出中。如果该项目不存在,则它会放入Not found输出...
$output = [];
foreach ( $colldata2 as $item ) {
$output[$item] = $colldata[$item] ?? 'Not found';
}
print_r($output);
给..
Array
(
[bench-press-rod] =>
[adidas-classic-backpack] => 93549559913
)
TA贡献1934条经验 获得超2个赞
检查这个。
$colldata=array("bench-press-rod"=>'',"adidas-classic-backpack"=>'93549559913',"adidas-classic-backpack-legend-ink-multicolour"=>'',"puma-suede-classic-regal"=>'93549920361,93549723753');
$colldata2=array(0 => 'bench-press-rod',1 => 'adidas-classic-backpack');
$result = [];
foreach ($colldata2 as $key => $value) {
if (array_key_exists($value, $colldata)) {
array_push($result,$colldata[$value]);
}
}
echo '<pre/>';
print_r($result);
- 3 回答
- 0 关注
- 160 浏览
添加回答
举报