php 从关联数组中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1536827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting values from associative array
提问by Elitmiar
I have the following main array called $m
我有以下主数组称为 $m
Array
(
[0] => Array
(
[home] => Home
)
[1] => Array
(
[contact_us] => Contact Us
)
[2] => Array
(
[about_us] => About Us
)
[3] => Array
(
[feedback_form] => Feedback Form
)
[4] => Array
(
[enquiry_form] => Products
)
[5] => Array
(
[gallery] => Gallery
)
)
I have the values eg home, contact_us in a array stored $options , I need to get the values from the main array called $m using the $options array
我在存储 $options 的数组中有 home、contact_us 等值,我需要使用 $options 数组从名为 $m 的主数组中获取值
eg. If the $options array has value home, i need to get the value Home from the main array ($m)
例如。如果 $options 数组的值为 home,我需要从主数组 ($m) 中获取值 Home
my code looks as follows
我的代码如下
$c = 0;
foreach($options as $o){
echo $m[$c][$o];
++$c;
}
I somehow just can't receive the values from the main array?
我不知何故无法从主数组接收值?
回答by Luká? Lalinsky
I'd first transform $mto a simpler array with only one level:
我首先转换$m为一个只有一个级别的更简单的数组:
$new_m = array();
foreach ($m as $item) {
foreach ($item as $key => $value) {
$new_m[$key] = $value;
}
}
Then you can use:
然后你可以使用:
foreach ($options as $o) {
echo $new_m[$o];
}
回答by Lizard
Try this:
尝试这个:
foreach($options as $o){
foreach($m as $check){
if(isset($check[$o])) echo $check[$o];
}
}
Although It would be better TO have the array filled with the only the pages and not a multidimensional array
虽然最好让数组填充唯一的页面而不是多维数组
回答by stroop
Assuming keys in the sub arrays are unique you can
假设子数组中的键是唯一的,你可以
- merge all sub arrays into a single array using call_user_func_array on array_merge
- swap keys and values of your option array
- Use array_intersect_key to retrieve an array with all the values.
- 在 array_merge 上使用 call_user_func_array 将所有子数组合并为一个数组
- 交换选项数组的键和值
- 使用 array_intersect_key 检索包含所有值的数组。
Example like so:
像这样的例子:
$options = array('about_us', 'enquiry_form');
$values = array_intersect_key(
call_user_func_array('array_merge', $m), // Merge all subarrays
array_flip($options) // Make values in options keys
);
print_r($values);
which results in:
这导致:
Array
(
[about_us] => About Us
[enquiry_form] => Products
)
回答by Rahul rajoria
$trails1 = array();
foreach ($trails as $item) {
foreach ($item as $key => $value) {
$trails1[].= $value;
}
}
echo '<pre>';print_r($trails1);
exit;
回答by voidstate
How's this?
这个怎么样?
foreach( $options as $option )
{
foreach( $m as $m_key => $m_value )
{
if( $option == $m_key )
{
echo 'Name for ' . $options . ' is ' . $m_value;
break;
}
}
}
回答by Eineki
Are you sure that the options array is in the same order of $m? Maybe you your
您确定选项数组与 $m 的顺序相同吗?也许你你的
echo $m[$c][$o];
is resolving into a $m[0]['gallery'] which is obviously empty.
解析为 $m[0]['gallery'] 显然是空的。
you can try different solutions, to me, a nice one (maybe not so efficient) should be like this:
你可以尝试不同的解决方案,对我来说,一个不错的解决方案(可能效率不高)应该是这样的:
for($c=0, $limit=count($c); $c < $limit; $c++)
if (array_search(key($m[$c]), $options))
echo current($m[$c]);
If you would like to use your approach have to flatten your array with something like this:
如果你想使用你的方法必须用这样的东西来展平你的数组:
foreach ($m as $o)
$flattenedArray[key($o)]=current($o);
foreach ($options as $o)
echo $flattenedArray($o);
Doing this, though, eliminates duplicate voices of your original array if there are such duplicates.
但是,如果有这样的重复,这样做会消除原始阵列的重复声音。
回答by clops
Try using a recursive array_walk function, for example
例如,尝试使用递归 array_walk 函数
$a = array(
array('ooo'=>'yeah'),
array('bbb'=>'man')
);
function get_array_values($item, $key){
if(!is_numeric($key)){
echo "$item\n";
}
}
array_walk_recursive($a,'get_array_values');

