tags:

views:

238

answers:

6

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?? Please help

A: 

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;
    }
  }
}
voidstate
+2  A: 

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

Lizard
+4  A: 

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];
}
Lukáš Lalinský
A: 

Are you sure that the options array is in the same order of $m? Maybe you your

echo $m[$c][$o];

is resolving into a $m[0]['gallery'] which is obviously empty.

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.

Eineki
A: 

Try using a recursive array_walk function, for example

$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');
clops
You can effectively twist the function to create whatever you want from a tree-like array structure.
clops
+1  A: 

Assuming keys in the sub arrays are unique you can

  1. merge all sub arrays into a single array using call_user_func_array on array_merge
  2. swap keys and values of your option array
  3. Use array_intersect_key to retrieve an array with all the values.

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
)
stroop