0

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++;
        }

where

$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"
]

And

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

But it's return error Undefined offset: 2.

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

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.

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++;
        }
Script Lover
  • 331
  • 2
  • 6
  • 15
  • Just to check - what would you expect `$map_array` to look like in the end. – Nigel Ren Jan 17 '19 at 07:55
  • You should test for the presence of the item in the array with isset($Fresh_Record['date'][$index]) before attempting to reference it. – SPlatten Jan 17 '19 at 07:55
  • Why not just add all the dates and array_unique it? – Andreas Jan 17 '19 at 07:57
  • 2
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – executable Jan 17 '19 at 07:58
  • @SPlatten I did your suggested solution but it is returning this error `syntax error, unexpected 'return' (T_RETURN), ` – Script Lover Jan 17 '19 at 09:17
  • @ScriptLover, can you show your modified code? – SPlatten Jan 17 '19 at 10:00
  • @SPlatten I have edited check the question – Script Lover Jan 17 '19 at 10:01
  • @ScriptLover, your original code is wrong, you test for count being larger than the array then index into it anyway. – SPlatten Jan 17 '19 at 10:07
  • @SPlatten can you guide me how can I fix it – Script Lover Jan 17 '19 at 10:08
  • Change if(isset($Fresh_Record['date'][$index])) to if(!isset($Fresh_Record['date'][$index])), then in your code change $map_array['date'] = $Fresh_Record['date'][$index]; to $Fresh_Record['date'][$index] = $single; – SPlatten Jan 17 '19 at 10:11

1 Answers1

-1

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
    ]
]