首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么它是我的数组一个3D而不是一个2D?

为什么它是我的数组一个3D而不是一个2D?
EN

Stack Overflow用户
提问于 2016-11-26 16:52:13
回答 1查看 35关注 0票数 0

我有点搞砸了我的php,只想得到一些帮助。目前,我的代码接受从下拉框中选择的值,并将这些值连同它们对应的行一起放在一个表中,放入一个3D数组中。然而,它只需要一个二维数组,但我不知道如何摆脱三维数组的第一个维度。Php代码:

代码语言:javascript
复制
<?php
function startmatching(){
    define( "DB_DSN", "mysql:host=localhost;dbname=FoodMatching");
    define( "DB_USERNAME", "root");
    define( "DB_PASSWORD", "" ); 

    // define the empty array to be filled from db

    $results = array();
    // any other php tasks that dont needthe ingcats
    // store sql

    $IngName = $_POST['IngredientName'];
    $IngCounter=0;
    while($IngCounter<count($IngName)){
        $sSQL = "SELECT IngID, IngName, Texture, Colour, Bitter, Sweet, Sour, Salty, Umami FROM IngredientCharacteristics WHERE IngName='$IngName[$IngCounter]'";
        // create an instance of the connection
        $conn   = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
        // prepare
        $st   = $conn->prepare( $sSQL );
        // execute the connection
        $st->execute();
        $counter=0;
        // while myslq has rows loop over them and store
        while($row = $st->fetch() ){
            $results[$counter][] = $row;
            $counter++;
        }
        $IngCounter++;
    }
    print_r($results);
}
?>

html:

代码语言:javascript
复制
 <form method='post' role="form" autocomplete="off">

    <div class="entry input-group col-xs-3">
        <?php //this function takes the ing id and ingname from ingredientcharacteristics table.
                    // if there are results stored create the select and loop over
                    if(!empty($aIngList)){
                        echo "<span class='form-control' style = 'float:left; font-size: 20px;font-family:'Helvetica Neue' ><select name = 'IngredientName[]' class = 'custom-dropdown__select custom-dropdown__select--white'>";
                        //class = 'custom-dropdown custom-dropdown--white'
                        echo "<option value='' default >Choose an Ingredient</option>";
                        foreach ($aIngList as $iIngID => $sIngName) {
                            echo "<option value='".$sIngName."' >".$sIngName."</option>";
                        }
                        echo "</select></span>";
                    }else{
                        echo "<p>No results available!</p>";
                    }
                ?>

        <span class="input-group-btn">
            <button class="btn btn-success btn-add" type="button">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
            <button type="submit" class="hidden" name="startmatch" value ="startmatch" id="submit-form"></button>
        </span>
    </div>


</form>

输出$results提供了以下内容:

代码语言:javascript
复制
Array

    (
        [0] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [IngID] =&gt; 2
                        [0] =&gt; 2
                        [IngName] =&gt; Apples Fresh
                        [1] =&gt; Apples Fresh
                        [Texture] =&gt; 4
                        [2] =&gt; 4
                        [Colour] =&gt; 8
                        [3] =&gt; 8
                        [Bitter] =&gt; 7
                        [4] =&gt; 7
                        [Sweet] =&gt; 3
                        [5] =&gt; 3
                        [Sour] =&gt; 4
                        [6] =&gt; 4
                        [Salty] =&gt; 8
                        [7] =&gt; 8
                        [Umami] =&gt; 9
                        [8] =&gt; 9
                    )

                [1] =&gt; Array
                    (
                        [IngID] =&gt; 2
                        [0] =&gt; 2
                        [IngName] =&gt; Apples Fresh
                        [1] =&gt; Apples Fresh
                        [Texture] =&gt; 4
                        [2] =&gt; 4
                        [Colour] =&gt; 8
                        [3] =&gt; 8
                        [Bitter] =&gt; 7
                        [4] =&gt; 7
                        [Sweet] =&gt; 3
                        [5] =&gt; 3
                        [Sour] =&gt; 4
                        [6] =&gt; 4
                        [Salty] =&gt; 8
                        [7] =&gt; 8
                        [Umami] =&gt; 9
                        [8] =&gt; 9
                    )

            )

    )

我不知道如何摆脱额外的数组,因此任何帮助都是非常感谢的:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-26 18:19:41

问题是这条线:

代码语言:javascript
复制
$results[$counter][] = $row;

解决办法是将它更改为

代码语言:javascript
复制
$results[] = $row;

PHP将自动将$row附加到$results中,这意味着您根本不需要$counter变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40820692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档