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

Categories

0 votes
426 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...