Getting values from associative array

前端 未结 7 981
孤独总比滥情好
孤独总比滥情好 2020-12-17 09:23

I have the following main array called $m

Array
(
    [0] => Array
        (
            [home] => Home
        )

    [1] => Arra         


        
7条回答
  •  庸人自扰
    2020-12-17 09:56

    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
    )
    

提交回复
热议问题