php - 将数组键与数组值 PHP 匹配

标签 php arrays

如何匹配数组键和数组值。 with array_intersect() 是匹配key数组。 但是如何将第一个数组中的键与第二个数组中的值匹配。

例如数组:

$value_array=array(
'1'=>'text one',
'2'=>'text two',
'3'=>'text three',
'4'=>'text four',
'5'=>'text five',
'6'=>'text six',
'7'=>'text seven',
'8'=>'text eight',
'9'=>'text nine',
'10'=>'text ten'
);

$key_array=array(
'1'=>'1',
'2'=>'2',
'3'=>'4',
'4'=>'5',
'5'=>'7'
);

如果使用array_intersect(),用于匹配键。我想搜索键数组并获取值数组。输出将如下所示:

Array (
        [1] => text one 
        [2] => text two 
        [3] => text four 
        [4] => text five 
        [5] => text seven 
      )

最佳答案

我得到了答案,就在这里:

print_r(array_intersect_key($value_array, array_flip($value_key)));

输出将显示:

    Array ( 
[1] => text one 
[2] => text two 
[4] => text four 
[5] => text five 
[7] => text seven 
)

关于php - 将数组键与数组值 PHP 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030669/

相关文章:

php - 带有 codeigniter 问题的数据库缓存

javascript - 如何为 XMLHttpRequest 目的在 Javascript 中存储 $_SESSION 变量?

javascript - 延迟循环遍历数组内容

java - 何时在递归中使用 ArrayList 而不是数组

php - Codeigniter 购物车 - 在披萨上添加番茄酱

php - ' fatal error 无法创建锁定文件 : Bad file descriptor (9)' while running php artisan server

php - PHP 如何自然地预测变量中的撇号?

javascript - 循环遍历数组中的第一层并将其分配给变量

javascript - 语法错误: missing ) after argument list in javascript function with json response

javascript - JavaScript 中的数组是用什么数据结构实现的?