如何修复未定义的偏移量:2? [英] How to fix Undefined offset: 2?

查看:107
本文介绍了如何修复未定义的偏移量:2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组项推到另一个数组.

I am trying to push an item of array to another array.

$index = 0;
        foreach($d as $single){

            if(!in_array($single,$Fresh_Record['date'])){
                if(count($Fresh_Record['date']) >= $index){
                    $map_array['date'] = $Fresh_Record['date'][$index];
                    $map_array['counter'] = 0;
                }
            }

            $index++;
        }

其中

$d =  [
  0 => "2019-01-17"
  1 => "2019-01-16"
  2 => "2019-01-15"
  3 => "2019-01-14"
  4 => "2019-01-13"
  5 => "2019-01-12"
  6 => "2019-01-11"
]

还有

$Fresh_Record =  [
    "date" => array:2 [
        0 => "2019-01-10"
        1 => "2019-01-14"
    ]
    "counter" => array:2 [
        0 => 1000.0
        1 => 500.0
    ]
]

但是它返回错误Undefined offset: 2.

实际上,我试图将日期从$d存储到$map_array['date']中,而不是$Fresh_Record['date'].

Actaully I am trying to store dates into $map_array['date'] from $d which are not it $Fresh_Record['date'].

counter相同,正如您在数组中看到的那样.所以日期在$Fresh_Record['date']中不可用,然后我想将$d中的日期添加到 $map_array['date']以及计数器0.

Also same thing with the counter, as you can see in the array. So date not available in $Fresh_Record['date'] then I want to add the date from $d to $map_array['date'] and also counter 0.

@SPlatten评论后

After @SPlatten Comment

$index = 0;
        foreach($d as $single){

            if(!in_array($single,$Fresh_Record['date'])){

                if(isset($Fresh_Record['date'][$index]))
                    $map_array['date'] = $Fresh_Record['date'][$index];
                } 
            }

            $index++;
        }

推荐答案

您的$Fresh_Record数组有问题.数组格式不正确.看起来应该像这样

There's a problem with your $Fresh_Record array. The array is not correctly formed. It should look like this

$Fresh_Record =  [
    "date" => [
        0 => "2019-01-10"
        1 => "2019-01-14"
    ]
    "counter" => [
        0 => 1000.0
        1 => 500.0
    ]
]

这篇关于如何修复未定义的偏移量:2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆