在PHP中合并数组 [英] Combine arrays in PHP

查看:33
本文介绍了在PHP中合并数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习PHP中的数组.为了简短起见,发布了一些摘要.

Trying to learn arrays in PHP. Snippets posted for brevity.

此处的HTML表单:

<p>What are your favorite type of cookies?</p> 
<input type="checkbox" name="cookies[]" value="Oreos" />Oreos<br />
<input type="checkbox" name="cookies[]" value="Chocolate chip" />Chocolate chip<br />
<input type="checkbox" name="cookies[]" value="Sugar" />Sugar<br />
<input type="checkbox" name="cookies[]" value="Vanilla Mocha" />Vanilla Mocha<br /> 

<p>What are your favorite type of drinks?</p>
<input type="checkbox" name="drinks[]" value="Soda" />Soda<br />
<input type="checkbox" name="drinks[]" value="Wine" />Wine<br />
<input type="checkbox" name="drinks[]" value="Milk" />Milk<br />
<input type="checkbox" name="drinks[]" value="Water" />Water<br /> 

此处的PHP页面:

foreach ($drinks as $d) {
    echo "Your favorite drink(s) are: " . $d . "<br />"; 
}

foreach ($cookies as $cookie) {
    echo "Your favorite cookies are: " . $cookie . "<br />"; 
} 

$experimentalArray = array($cookie => $d); 

foreach ($experimentalArray as $key => $value) { 
    echo "Cookie - " . $key . " Drink - " . $value . "<br /><br />"; 
} 

饼干和饮料都是多项选择题,因此您可以选择多个答案.

Both cookies and drinks are multi-choice questions, so you can select more than one answer.

但是,experimentalArray仅显示在饮料和曲奇问题中选择的最后一个答案.

However, the experimentalArray only shows the last answer chosen in both drink and cookie question.

例如,我在饼干中选择Oreos和Chocolate Chip,在饮料中选择Soda和Wine.

For example, I choose Oreos and Chocolate Chip in cookies, and Soda and Wine in drinks.

答案是:饼干-巧克力片饮料-葡萄酒"

The answer comes out as: "Cookie - Chocolate chip Drink - Wine"

为什么不显示所有值?

针对多维脚本进行了编辑

<?php 

$drinks = $_POST['drinks']; 
$cookies = $_POST['cookies']; 

    $combinedArray = array( 'Cookies' => $cookies, 'Drinks' => $drinks); 

    foreach($combinedArray as $snackType => $snack) {
    print "<h2>$snackType</h2>";
    foreach ($snack as $number => $snackChosen) {
        print " $number is $snackChosen<br />";
    }

}?>

好吧,所以尝试改用多维数组脚本,因为先前的脚本不会按照HTML表单获取所有值.

Ok, so tried to do a multi-dimensional array script instead since the previous script wasn't going to obtain all the values as per the HTML form.

此脚本有效(已从书中摘录并在此处对此代码进行了修改),但是$ number的值从0开始.如何修改使其从1开始呢?

This script works (was ripped off from a book and modified for this code here), however, $number value starts at 0. How do I modify that so that it starts at 1 instead?

此外,这种用于进行多维数组的形式正确吗?可以用更好的方式重写它吗?

Also, is this proper form for doing multi-dimensional array? Could it have been rewritten in a better way?

再次感谢您的所有回复.即使我不太了解它们!:)因此,也感谢您的耐心配合.

And again, thank you for all responses. Even if I don't quite understand them! :) So, thank you for your patience as well.

推荐答案

不知道您使用的是哪个php教程,但是停止使用它,它可能已经过时了!

No idea which php tutorial you are using, but stop using it, it's probably horribly outdated!

您必须使用 $ _ POST / $ _ GET / $ _ REQUEST (包含POST,GET和-取决于配置)-Cookie值)数组. register_globals 已过时,如果启用,则可能存在安全漏洞.

You'll have to use the $_POST / $_GET / $_REQUEST (contains POST, GET and - depending on the config - Cookie values) arrays. register_globals is deprecated and a potential security hole if enabled.

例如,使用 $ drinks = isset($ _ POST ['drinks'])吗?$ _POST ['drinks']:array(); 获得$ drinks数组.相同的$ cookies.

For example, use $drinks = isset($_POST['drinks']) ? $_POST['drinks'] : array(); to get your $drinks array. Same for the $cookies one.

关于阵列问题.看起来您想要来自Drinks数组的键和来自hte cookies数组的值.如果是,请查看 array_combine().请注意,尽管要求两个数组具有相同数量的元素,但是将其提供给用户生成的长度可变的数组并不是一个好主意.

About your array issue. It looks like you want the keys from the drinks array and the values from hte cookies array. If yes, have a look at array_combine(). Note that it requires both arrays to have the same amount of elements though so feeding it with user-generated arrays where the length can vary is not a very good idea.

Fyi, $ experimentalArray = array($ cookie => $ d); 将$ cookies的最后一个元素映射到$ drinks的最后一个元素,因为PHP没有块范围,因此$ cookie和$ d仍然指向您在foreach循环中获得的最后一个数组元素.

Fyi, $experimentalArray = array($cookie => $d); maps the last element from $cookies to the last element of $drinks because PHP has no block scope and thus $cookie and $d still point to the last array elements you got in your foreach loops.

这篇关于在PHP中合并数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆