简体   繁体   English

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

[英]Extract a single value from two arrays that share the same one in php?

$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"); 

How do I extract only Olivia Hussey from the array using a function? 如何使用函数从数组中仅提取Olivia Hussey? Basically, how do I combine the two columns and then output a single value for Olivia Hussey using a function in php? 基本上,我如何结合两列,然后使用php中的函数为Olivia Hussey输出单个值?

I've tried the following but it is not working: 我已经尝试了以下方法,但无法正常工作:

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

Thanks. 谢谢。

According to the the documentation , the keys are from the *first" array, not the second. So this might work: 根据文档 ,键来自* first“数组,而不是第二个。因此这可能有效:

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

array_intersect() and reset() is the way to go: array_intersect()reset()是方法:

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

Demo 演示版


array_intersect() returns an array containing all the values of array1 that are present in all the arguments. array_intersect()返回一个数组,其中包含所有自变量中存在的所有array1值。 Note that keys are preserved. 请注意,密钥被保留。


reset() rewinds array's internal pointer to the first element and returns the value of the first array element. reset()数组的内部指针后退到第一个元素,并返回第一个数组元素的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM