简体   繁体   English

如何从三维表单输入名称数组获取数据

[英]How to get data from a three dimension form input name array

I have a form below, 我下面有一个表格,

The select name contains the fixtureId, whether it's home or away and the teamId fixture[id][home/away][teamid] and the selected value is the score: 选择名称包含fixtureId(无论是在家还是在外)和teamId fixture[id][home/away][teamid] ,所选值为得分:

HTML 的HTML

<form>
    <select name="fixture[1][home][5]">
        <option value="0">0</option>
        <option value="1">1</option>
    </select>
    <select name="fixture[1][away][10]">
        <option value="0">0</option>
        <option value="1">1</option>
    </select>
    <button type="submit"></button>
</form>

and I want to access the select name values combined with the selected value to output this in two foreach loops: 我想访问选择名称值和选择的值,以在两个foreach循环中将其输出:

echo $fixtureId."-".$venue."-".$teamId."-".$score;

PHP 的PHP

foreach($_POST['fixture'] as $fixtureId => $fixtureArray){

    foreach($fixtureArray as $venue => $scoreArray){

        echo $fixtureId."-".$venue;

        $teamId =  key($scoreArray[0]); 
        $score = $scoreArray[0];    

    }   
}

$scoreArray $ scoreArray

Array
(
    [10] => 0
)

The Problem 问题

At the moment $fixtureId and $venue work as expected but I cannot get the score and Team Id even though they are in the $scoreArray ?! 目前$fixtureId$venue可以按预期工作,但是即使它们在$scoreArray中,我也无法获得比分和团队ID?

Team Id should be 10 and Score should be 0; 小组ID应为10,分数应为0;

I will bet you are not going deep enough. 我敢打赌你还不够深入。 Try this before your foreach() loop: 在您的foreach()循环之前尝试以下操作:

echo '<pre>'.print_r($_POST['fixture'], true).'</pre>';

Lets assume you have choosen the first option and we are dealing with first select ... 假设您选择了第一个选项,而我们正在处理第一个选择...

foreach($_POST['fixture'] as $fixtureId => $fixtureArray){
// Now $textureId = 1; $fixtureArray = array("home"=> array(5=> 0));

    foreach($fixtureArray as $venue => $scoreArray){
    // Now $venue = "home"; $scoreArray = array(5 => 0); therefore ...

        echo $fixtureId."-".$venue;

        // There is NO index 0, so there is no key for it
        $teamId =  key($scoreArray[0]); 
        // Again, There is NO index 0
        $score = $scoreArray[0];    

    }   
}

Use print_r everytime you are not sure about the indexes of your array. 每次不确定数组的索引时,请使用print_r

To fix this : Why don't you simply use another foreach ? 解决此问题的方法 :为什么不简单地使用另一个foreach

foreach($_POST['fixture'] as $fixtureId => $fixtureArray){
    print_r($fixtureArray)

    foreach($fixtureArray as $venue => $scoreArray){
        print_r($scoreArray);
        echo $fixtureId."-".$venue;

        foreach($scoreArray as $teamId => $score)
        {
            echo($teamId."-".$score);
        }
    }   
}

try this I've tested this code 试试这个我已经测试了这段代码

echo '<pre>';print_r($_POST);echo '</pre>';
    foreach($_POST['fixture'] as $fixtureId => $fixtureArray) {
        foreach($fixtureArray as $venue => $scoreArray) {
            echo '<pre>';print_r($scoreArray);echo '</pre>';
            //echo $fixtureId."-".$venue;
            //echo $scoreArray;
            foreach ($scoreArray as $scoreValue) {
                echo "fixtureId ".$fixtureId."-"." venue ".$venue." score value ".$scoreValue."<br />";
            }
            /*$teamId =  key($scoreArray[0]);
            $score = $scoreArray[0];*/

        }
    }

This is your code I just update few things. 这是您的代码,我只是更新了几件事。 It worked. 有效。

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

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