从关联数组获取值 [英] Getting values from associative array

查看:75
本文介绍了从关联数组获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下名为$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
        )

)

我在存储$ options的数组中有home,contact_us等值,我需要使用$ options数组从名为$ m的主数组中获取值

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,我需要从主数组($ m)中获取值Home

eg. If the $options array has value home, i need to get the value Home from the main array ($m)

我的代码如下

                    $c = 0;
                    foreach($options as $o){
                        echo $m[$c][$o];
                        ++$c;
                    }

我以某种方式无法从主数组接收值?

I somehow just can't receive the values from the main array?

推荐答案

我首先将$m转换为只有一个级别的简单数组:

I'd first transform $m to a simpler array with only one level:

$new_m = array();
foreach ($m as $item) {
    foreach ($item as $key => $value) {
        $new_m[$key] = $value;
    } 
}

然后您可以使用:

foreach ($options as $o) {
    echo $new_m[$o];
}

这篇关于从关联数组获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆