doucepei5298 2016-06-27 18:42
浏览 31
已采纳

数组的输出总是等于'i'

I have the variable $request. If i do vardump($request), I get the output of:

array(7) { 
    ["controller"]=> string(5) "index" 
    ["action"]=> string(5)"index" 
    ["module"]=> string(7) "default" 
    [2]=> array(8) { 
        ["g_goal_list_id"]=> string(3) "127" 
        ["textgoal"]=> string(9) "eats food" 
        ["task_0"]=> string(1) "0" 
        ["value_0"]=> string(5) "pukes" 
        ["task_1"]=> string(1) "0" 
        ["value_1"]=> string(0) "" 
        ["task_2"]=> string(1) "0"
        ["value_2"]=> string(0) "" 
    } 
    [3]=> array(10) { 
        ["g_goal_list_id"]=> string(3) "128" 
        ["textgoal"]=> string(9) "goes home" 
        ["task_0"]=> string(1) "0" 
        ["value_0"]=> string(20) "but never comes back" 
        ["task_1"]=> string(1) "0" 
        ["value_1"]=> string(14) "stays home now" 
        ["task_2"]=> string(1) "0" 
        ["value_2"]=> string(0) "" 
        ["task_3"]=> string(1) "0" 
        ["value_3"]=> string(0) "" 
    } 
    ["submit"]=> string(4) "Save" 
    ["task"]=> string(1) "5" 
}

which is all correct. However, I'm trying to use a foreach statment to grab values from the $request array and put them into a data array, and then submit that to the mysql db...

    foreach($request as $currentrow){
        //skips row if the field is empty
        if(strlen($currentrow['value']) < 1)//need to make sure I've defined $currentrow['value']
            continue;//skips row with empty field

        //I only need to grab the value/list_id/account_id from the form    
        $data = array('value' => $currentrow['value'],
            'g_goal_list_id' => $currentrow['g_goal_list_id'],
            'account_id' => g_getAccountId(),
            );
        var_dump($data);                

However, when I var_dump($data); my output looks like this:

array(3) { ["value"]=> string(1) "i" ["g_goal_list_id"]=> string(1) "i" ["account_id"]=> string(1) "1" }

array(3) { ["value"]=> string(1) "S" ["g_goal_list_id"]=> string(1) "S" ["account_id"]=> string(1) "1" }

array(3) { ["value"]=> string(1) "5" ["g_goal_list_id"]=> string(1) "5" ["account_id"]=> string(1) "1" }

The only thing that is correct in that var_dump($data) is the ["account_id"]

I'm thinking that my loop is incorrect, and I'm pretty bad with loops. Sooooo yeah, hopefully I included enough information. Thank you.

  • 写回答

3条回答 默认 最新

  • dongzhun6952 2016-06-28 18:44
    关注

    Huge thanks to @MonkeyZeus. I kept on getting an array that had the proper keys but all the values were null. Turns out I just needed to put my var_dump($data) inside of my loop, because when it was on the outside it was returning only the last array it looped thorough. On my form the last values it loops through were empty fields, that are meant for the user to add input. Also, I had to change 'g_goal_list_id' => $currentrow[$k]['g_goal_list_id'] to just 'g_goal_list_id' => $currentrow['g_goal_list_id']

    foreach ($request as $k=> $currentrow) {
            $hit = false;
            $data = array();
    
            if(is_array($currentrow)){
                foreach ($currentrow as $k2 => $v2) {
                    $explode = explode('_', $k2); //blows off the suffix of the $key
                    //var_dump($explode);
                    //$explode=substr($explode,2);
                    //echo $k2;
                    //echo '******';
                    //echo $v2;
                    echo $explode[0];
                    echo '/';
    
                    if($explode[0] == 'value'){
                        //echo "[" . $currentrow['g_goal_list_id'] . "]";
                        $hit = true;
                        $data = array(
                            'value' => $v2,
                            'g_goal_list_id' => $currentrow['g_goal_list_id'],
                            'account_id'=> g_getAccountId(),
                            );
                        continue;
                    }
                var_dump($data);
                }
    
            }
    
            if ($hit === false){
                continue;
            }
    
            break;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入