Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

php - Getting values from associative array

I have the following main array called $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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    } 
}

Then you can use:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...