2016-07-13 83 views
2

傢伙,我有多維數組我與這個以下數組得到它從$menu_ordervar_dump如何在笨返回多維數組

array(5) { 
[0]=> 
array(1) { 
    [0]=> 
    array(1) { 
    ["variant_name"]=> 
    string(5) "Spicy" 
    } 
} 
[1]=> 
array(2) { 
    [0]=> 
    array(1) { 
    ["variant_name"]=> 
    string(5) "Spicy" 
    } 
    [1]=> 
    array(1) { 
    ["variant_name"]=> 
    string(5) "small" 
    } 
} 
[2]=> 
array(2) { 
    [0]=> 
    array(1) { 
    ["variant_name"]=> 
    string(5) "Salty" 
} 
    [1]=> 
    array(1) { 
    ["variant_name"]=> 
    string(6) "medium" 
    } 
} 
[3]=> 
array(2) { 
    [0]=> 
    array(1) { 
    ["variant_name"]=> 
    string(12) "Mix of Herbs" 
    } 
    [1]=> 
    array(1) { 
    ["variant_name"]=> 
    string(5) "large" 
    } 
} 
[4]=> 
array(0) { 
} 
} 

這個數組,我需要得到variant_name成爲variant_menu_id本以下代碼:

foreach ($menu_order as $item) { 
      if (isset($item[0]["variant_name"])) { 
       foreach($item as $value) { 
        $variant_id[] = $this->Main_home_m->m_get_choice_id($value["variant_name"]); 
       } 
      } else { 
       $variant_id[] = array(); 
      } 
     } 

m_get_choice_id模型有這樣下面的代碼:

Function m_get_choice_id($variant_name){ 
    $this->db->select("variant_menu_id"); 
    $this->db->from("uhd_variant_menu"); 
    $this->db->where("variant_name",$variant_name); 
    $query = $this->db->get(); 
    return $query->row_array(); 
} 

variant_id會回到這個多維數組:

array(8) { 
    [0]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "3" 
    } 
    [1]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "3" 
    } 
    [2]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "6" 
    } 
    [3]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "4" 
    } 
    [4]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "7" 
    } 
    [5]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "5" 
    } 
    [6]=> 
    array(1) { 
    ["variant_menu_id"]=> 
    string(1) "8" 
    } 
    [7]=> 
    array(0) { 
    } 
} 

,但我想要的結果variant_id成爲這個多維數組:

array(5) { 
    [0]=> 
    array(1) { 
    [0]=> 
    string(1) "3" 
    } 
    [1]=> 
    array(2) { 
    [0]=> 
    string(1) "3" 
    [1]=> 
    string(1) "6" 
    } 
    [2]=> 
    array(2) { 
    [0]=> 
    string(1) "4" 
    [1]=> 
    string(1) "7" 
    } 
    [3]=> 
    array(2) { 
    [0]=> 
    string(1) "5" 
    [1]=> 
    string(1) "8" 
    } 
    [4]=> 
    array(0) { 
    } 
} 

傢伙你能幫助我如何獲得多維陣列?

謝謝(:

+0

只需先訪問變體ID,然後按下它即可 – Ghost

+0

你能告訴我怎麼做嗎?@Ghost – Kelvin

回答

1

或者,你可以創建一個臨時容器盛裝的ID使用數組讓他們都作爲一個數組後,按父容器內部的整批:

$result = array(); 
foreach ($menu_order as $item) { 
    $temp = array(); // initialize temporary storage 
    if (isset($item[0]["variant_name"])) { 
     foreach($item as $value) { 
      $variant = $this->Main_home_m->m_get_choice_id($value["variant_name"]); 
      $temp[] = $variant['variant_menu_id']; // push single id into temporary storage 
     } 
    } 
    $result[] = $temp; // push ending batch 
} 
+0

謝謝!它的工作!我會嘗試學習你做了什麼(: – Kelvin

+0

@凱爾文確定很高興這有幫助 – Ghost