如何在代码点火器中返回多维数组



>伙计们,我有多维数组,我从var_dump $menu_order中得到它,具有以下数组:

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) {
  }
}

伙计们你能帮我如何获得多维数组吗?

谢谢 (:

或者,您可以创建一个临时容器,其中包含一个数组的 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
}

相关内容

  • 没有找到相关文章

最新更新