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

时间:2016-06-27 18:42:26

标签: php arrays string loops

我有变量$request。如果我vardump($request),我得到输出:

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" 
}

这一切都是正确的。但是,我尝试使用foreach语句从$request数组中获取值并将它们放入data数组中,然后将其提交给mysql数据库...

    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);                

然而,当我var_dump($data);时,我的输出如下:

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" }

var_dump($data)唯一正确的是["account_id"]

我认为我的循环不正确,而且我对循环非常糟糕。 Sooooo是啊,希望我收集到足够的信息。谢谢。

3 个答案:

答案 0 :(得分:1)

你需要的是这样的东西:

foreach($request as $k=>$currentrow)
{
    $hit = false;
    $data = array();

    // Only look for sub-arrays
    if(is_array($currentrow))
    {
        foreach($currentrow as $k2=>$v2)
        {
            $explode = explode('_', $k2);

            if($explode[0] === 'value') //need to make sure I've defined $currentrow['value'] regardless of suffix
            {
                $hit = true;
                $data = array(
                    'value' => $v2,
                    'g_goal_list_id' => $currentrow[$k]['g_goal_list_id'],
                    'account_id' => g_getAccountId(),
                );

                continue;
            }
        }
    }

    if($hit === false)
    {
        continue;
    }

    var_dump($data);
}

答案 1 :(得分:0)

此元素:$currentrow['values']不存在(查看日志,您应该看到通知)。 $currentrow['g_goal_list_id']也没有。

循环遍历包含所有不同元素的数组,第一个是字符串,值为index。然后当你接受该字符串并使用values订阅它时,它会抛出一个警告,但然后获取第一个元素。

澄清一些CLI代码:

php > $a="abcd";
php > echo $a["foo"];
PHP Warning:  Illegal string offset 'foo' in php shell code on line 1
a

如果要循环键和值,请执行以下操作:

foreach($request as $key=>$value) {
}

话虽如此,我认为循环不是你想要的,你没有直接的表格数据。

答案 2 :(得分:0)

非常感谢@MonkeyZeus。我继续获得一个具有正确键的数组,但所有值都为null。结果我只需要将var_dump($data)放在我的循环中,因为当它在外面时它只返回它循环的最后一个数组。在我的表单上,它循环的最后一个值是空字段,这意味着用户可以添加输入。另外,我必须将'g_goal_list_id' => $currentrow[$k]['g_goal_list_id']更改为'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;
    }