如何在codeigniter中返回多维数组

时间:2021-08-06 21:22:00

guys i have multidimensional array which i got it from var_dump of $menu_order with this following array :

伙计们,我有多维数组,我从$ menu_order的var_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) {
}
}

from that array, i need to get the variant_name become variant_menu_id with this following code :

从该数组,我需要使用以下代码将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();
            }
        }

the model of m_get_choice_id have this following code :

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();
}

the variant_id will be return to this multidimensional 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) {
  }
}

but i want the result variant_id become this multidimensional array :

但我希望结果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) {
  }
}

guys can you help me how to get the multidimensional array?

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

thank you (:

谢谢 (:

1 个解决方案

#1


1  

Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:

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

#1


1  

Alternatively, you can create a temporary container holding the ids with an array. After getting them all as an array, push that whole batch inside a parent container:

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