简体   繁体   English

访问3D数组

[英]accessing a 3d array

my understanding of php and how to use multi-dimensional arrays is limited so i am struggling to understand how to access name="value". 我对php以及如何使用多维数组的理解是有限的,所以我在努力了解如何访问name =“ value”。

<input name='questions[]' type='text' value='question 1' /><br/>
  <input name='choice[1-1][]' type='text' value='option 1' />   
    <input name='value[1-1][1][]' type='text' value='value' /><br/>
  <input name='choice[1-1][]' type='text' value='option 2' />   
    <input name='value[1-1][2][]' type='text' value='value' /><br/>
  <input name='choice[1-1][]' type='text' value='option 3' />   
    <input name='value[1-1][3][]' type='text' value='value' /><br/>

I have used a foreach loop and access it that worked, but I want to access a single element in the value array. 我使用了一个foreach循环并访问了它,但我想访问值数组中的单个元素。 so I believe that using an alternate method would be better. 所以我相信使用替代方法会更好。

I have accessed questions[] by: 我通过以下方式访问了问题[]:

$q = $_POST['questions'];
echo $q[0];

but I tried to access choice[] and value[] in a similar way, but without success. 但是我尝试以类似的方式访问choice []和value [],但是没有成功。

Firstly I tried print_r() to see how the array was set up, I saw i needed extra [] for choice and two extra for value, but I added them and got the error message, Undefined offset: 0, which from research I have found means that array is not set, I am obviously accessing it incorrectly, can someone please advise. 首先,我尝试使用print_r()来了解如何设置数组,我看到我需要额外的[]作为选择,另外两个值作为附加值,但是我添加了它们并得到了错误消息,未定义的偏移量:0,这是我研究得到的发现意味着未设置数组,我显然是错误地访问了它,请有人指教。

Also advice on a good tutorial for multi-dimensional arrays can be found would be appreciated, ive already looked at php.net. 如果已经看过php.net,也可以找到有关多维数组的良好教程的建议。

Instead of telling you exact answer, let me guide you with the hint so that you can learn and get the fun yourself. 除了告诉您确切的答案外,我还指导您提示,以便您自己学习并获得乐趣。

use: 采用:

    print_r($_POST);

with the result you'll be able to decide how to call each element. 结果,您将能够决定如何调用每个元素。 If you still have problem just comment below. 如果仍有问题,请在下面评论。

I think this would be easier to process via php than your current structure 我认为通过php比您当前的结构更容易处理

<input name='questions[q1]' type='text' value='question 1' /><br/>
<input name='questions[choice][1]' type='text' value='option 1' />   
<input name='questions[value][1]' type='text' value='value' /><br/>
<input name='questions[choice][2]' type='text' value='option 1' />   
<input name='questions[value][2]' type='text' value='value' /><br/>  
<input name='questions[choice][3]' type='text' value='option 1' />   
<input name='questions[value][3]' type='text' value='value' /><br/>

i can see a few variations on this depending on your particular circumstances. 根据您的特定情况,我可以看到一些变化。

Basically this is how PHP will populate the array (*$_GET/$_POST -- depending on the form method*) when this form is submitted. 基本上,这就是提交表单时PHP将如何填充数组(* $ _ GET / $ _ POST-取决于表单方法*)的方式。 Anything inside of the square brackets is literal (with the exception of a few characters like under-scores, which are translated to dashes). 方括号内的所有内容均为文字(除下划线以外的一些字符除外,这些字符会转换为破折号)。 Empty brackets means PHP will automatically append each value to that array (starting from a 0 index) as if you used array_push in PHP. 空括号表示PHP将自动将每个值附加到该数组(从0索引开始),就像您在PHP中使用array_push

So your array would like this... 所以你的数组会这样...

$_POST['value']['1-1'][1][0] = 'whatever value the user supplied 1';
$_POST['value']['1-1'][2][0] = 'whatever value the user supplied 2';
$_POST['value']['1-1'][3][0] = 'whatever value the user supplied 3';

To access any of those elements in PHP you would use that same notation. 要访问PHP中的任何这些元素,您将使用相同的符号。 If you wanted to iterate over them you'll have to consider that they are three dimensional. 如果要遍历它们,则必须考虑它们是三维的。 So for example... 例如

foreach ($_POST['value']['1-1'] as $values) {
    foreach ($values as $value) {
        echo "$value\n";
    }
}

This would output 这将输出

whatever value the user supplied 1
whatever value the user supplied 2
whatever value the user supplied 3

Also, I forgot to mention that you can use something like var_dump($_POST, $_GET); 另外,我忘了提到您可以使用类似var_dump($ _ POST,$ _GET);的东西。 to look at a printable the structure of the array. 看一个可打印的数组结构。 I highly recommend var_dump for debugging variables, because it gives you a lot of useful information about those variables that goes above and beyond any other function like print_r/echo/etc... 我强烈建议您使用var_dump调试变量,因为它为您提供了有关这些变量的许多有用信息,这些变量超出了诸如print_r / echo / etc之类的任何其他函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM