从两个在php中共享相同数组的数组中提取单个值?

时间:2016-05-31 00:31:30

标签: php arrays function

$actresses = array(
    1 => "Natalie Wood",
    2 => "Audrey Hepburn",
    3 => "Marilyn Monroe",
    4 => "Grace Kelly",
    5 => "Olivia Hussey");

$actressestoday = array(
    6 => "Jennifer Lopez",
    7 => "Jennifer Love Hewitt",
    8 => "Paula Newman",
    9 => "Lindsay Lohan",
   10 => "Michelle Branch",
   11 => "Olivia Hussey"); 

如何使用函数从数组中仅提取Olivia Hussey?基本上,我如何组合两列,然后使用PHP中的函数为Olivia Hussey输出单个值?

我已尝试过以下操作,但它无效:

 $allactresses = array_intersect($actresses, $actressestoday);
 $value = array_column($allactresses, '11');

感谢。

2 个答案:

答案 0 :(得分:0)

根据documentation,键来自* first“数组,而不是第二个。所以这可能有效:

$allactresses = array_intersect($actresses, $actressestoday); 
$value = $allactresses[5];

答案 1 :(得分:0)

array_intersect()reset()是要走的路:

$result = reset(array_intersect($actressestoday, $actresses));
echo $result;
//Olivia Hussey

Demo

  

array_intersect()返回一个包含array1所有值的数组   在所有论点中都存在。请注意,密钥会被保留。

  

reset()将数组的内部指针倒回到第一个元素和   返回第一个数组元素的值。