Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 内爆二维返回值为';插入数据库时不相同(已编辑)_Php_Arrays_Algorithm_Codeigniter - Fatal编程技术网

Php 内爆二维返回值为';插入数据库时不相同(已编辑)

Php 内爆二维返回值为';插入数据库时不相同(已编辑),php,arrays,algorithm,codeigniter,Php,Arrays,Algorithm,Codeigniter,我有这样的二维数组: Array ( [0] => Array ( [0] => a [1] => d [2] => c [3] => e [4] => b ) [1] => Array ( [0] => c [1]

我有这样的二维数组:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => d
            [2] => c
            [3] => e
            [4] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => b
            [2] => e
            [3] => a
            [4] => d
        )

    [2] => Array
        (
            [0] => e
            [1] => c
            [2] => b
            [3] => d
            [4] => a
        )
)
我会把它变成一维,然后像这样内爆

a,d,c,e,b; c,b,e,a,d; e,c,b,d,a;
下面是我在模型中的代码(我使用PHP和CodeIgniter作为我的框架):

将二维数组转换为一维数组并将其转换为字符串

private function _randomize_answer($id)
{
    $alphabet = '';
    $answer = '';

    // Get from database
    // The result is ID question array
    $question = $this->select_temp_question($id)['question'];

    // $question output is ID questions list with semicolon as the delimeter
    // Output result is : 1; 2; 3; 4; 5; . . .
    $question = explode('; ', $question);

    // count($question) is = 50
    for ($i = 0; $i < count($question); $i++)
    {
        // Get from database
        // The output is 2-dimensional array
        $options[$i][0] = 'a' . $this->select_final_question($question[$i])['option_a'];
        $options[$i][1] = 'b' . $this->select_final_question($question[$i])['option_b'];
        $options[$i][2] = 'c' . $this->select_final_question($question[$i])['option_c'];
        $options[$i][3] = 'd' . $this->select_final_question($question[$i])['option_d'];
        $options[$i][4] = 'e' . $this->select_final_question($question[$i])['option_e'];

        // Shuffling array
        shuffle($options[$i]);

        // FOR looping, to get the first character / getting the alphabet
        for ($j = 0; $j < count($options[$i]); $j++)
        {
            $alphabet[$i][$j] = substr($options[$i][$j], 0, 1);
        }

    }

    // FOR looping, to convert from 2-dimensional array into 1-dimensional array
    // and convert again into string with comma as the glue
    // and semicolon as the delimeter per array
    // 
    // Output result is : a,d,c,e,b; c,b,e,a,d; e,c,b,d,a; . . . 
    for ($i = 0; $i < count($question); $i++)
    {
        $answer .= implode(',', $alphabet[$i]) . '; ';
    }

    // To remove the end of the semicolon
    $answer = substr($answer, 0, -2);

    // Return the answer as string
    return $answer;
}

这两个函数都放在同一个模型文件中。

您必须使用一种简单的调试技术—只需打印出SQL查询并查看它是否符合表模式

例如,如果列看起来像
answer varchar(9)not null默认值“”
, 每条记录最多可包含9个字符:

create temporary table tmp (
  a varchar(9) not null default ''
);

insert into tmp values ('a,d,c,e,b; c,b,e,a,d; e,c,b,d,a;');

show warnings
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'a' at row 1 |
+---------+------+----------------------------------------+

select * from tmp;
+-----------+
| a         |
+-----------+
| a,d,c,e,b |
+-----------+
1 row in set (0.00 sec)

您必须使用一种简单的调试技术——只需打印出SQL查询并查看它是否符合表模式

例如,如果列看起来像
answer varchar(9)not null默认值“”
, 每条记录最多可包含9个字符:

create temporary table tmp (
  a varchar(9) not null default ''
);

insert into tmp values ('a,d,c,e,b; c,b,e,a,d; e,c,b,d,a;');

show warnings
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'a' at row 1 |
+---------+------+----------------------------------------+

select * from tmp;
+-----------+
| a         |
+-----------+
| a,d,c,e,b |
+-----------+
1 row in set (0.00 sec)


你发布的代码是不相关的,只需显示插入的部分,你已经说过上面的代码工作正常,请注意,只需使用活动记录您可以
foreach
您的数组,并使用
array\u push
@Ghost创建一个新数组来包含它们。当我尝试回显控制器时,它工作正常,但当我尝试将其插入数据库时,成功插入数据库的记录只是第一个数组,所以您询问在数据库中插入数据的问题,但您没有向我们显示该代码?!有一个最简单的方法来解决你的问题。。。。您可以使用
insert\u batch()。。。这里有一个链接供您检查您发布的代码是否无关,只需显示插入的部分,您已经说过上面的代码工作正常,请注意,只需使用活动记录您可以
foreach
您的数组,并使用
array\u push
@Ghost创建一个新数组来包含它们。当我尝试回显控制器时,它工作正常,但当我尝试将其插入数据库时,成功插入数据库的记录只是第一个数组,所以您询问在数据库中插入数据的问题,但您没有向我们显示该代码?!有一个最简单的方法来解决你的问题。。。。您可以使用
insert\u batch()。。。这里有一个链接供您检查否,我想,因为我的数据类型是
varchar(1024)
,输入值大约为+-500,SQL查询是什么样子的?我会回答,但很抱歉,我稍后会发布它。谢谢你的建议:)我该怎么做?我想我应该打印查询还是什么?只要
echo$query
$this->db->insert
中就可以了,因为我的数据类型是
varchar(1024)
,输入的值大约是+-500,SQL查询是什么样子的?我会回答,但很抱歉,我稍后会发布它。谢谢你的建议:)我该怎么做?我应该打印查询还是什么?只需在
$this->db->insert中
echo$query