如何使用Codeigniter以此格式在视图中输出
brocolli 1/2 kilo
soy sauce 1 bottle
控制器:
$ingredient_name =implode(",",$_POST["mytext"]); //contains ex. [brocolli,soy sauce]
$ingredient_value = implode(",", $_POST["mytext2"]); //contains ex. [1/2 kilo, 1 bottle]...
答案 0 :(得分:1)
您应该在已发布字段值上显示的示例中使用explode。然后你可以使用array_combine:
$ingredient_name = explode(",",$_POST["mytext"]);
$ingredient_value = explode(",", $_POST["mytext2"]);
$result = array_combine($ingredient_name, $ingredient_value);
现在$ result是一个包含密钥及其各自值的数组。
答案 1 :(得分:0)
根据您提供的信息,我假设第一个数组包含成分列表,第二个数组包含成分数量,并且您希望将数组放入键/值对。
这应该可以解决问题:
$ingredients = [];
$ingredient_names = explode(",", $_POST['mytext']);
$ingredient_values = explode(",", $_POST['mytext2']);
// combine values into a single array
foreach ($ingredient_names as $i => $name) {
$ingredients[$name] = $ingredient_values[$i];
}
// output the ingredients however you want
echo "Ingredients:<br>";
foreach ($ingredients as $name => $amount) {
echo "{$name} {$amount}<br>";
}