Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...