Getting values from associative array

前端 未结 7 973
孤独总比滥情好
孤独总比滥情好 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:48
    $trails1 = array();
              foreach ($trails as $item) {
                  foreach ($item as $key => $value) {
                      $trails1[].= $value;
                  }
    
              }
              echo '<pre>';print_r($trails1);
            exit;
    
    0 讨论(0)
  • 2020-12-17 09:49

    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

    0 讨论(0)
  • 2020-12-17 09:55

    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.

    0 讨论(0)
  • 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
    )
    
    0 讨论(0)
  • 2020-12-17 09:58

    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');
    
    0 讨论(0)
  • 2020-12-17 10:03

    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];
    }
    
    0 讨论(0)
提交回复
热议问题