Search code examples
phparraysarray-push

Why are the strings not being added into the PHP array?


I am trying to create an array holding the dates of each title. However the $dates array is var_dumping int(1) rather than an array. And $dates[0] is NULL.

The csv files follow this formatting: log_2013_14_04.csv. The substr is working fine and cropping the date from the file, but why is the information not being added into the array?

$files = glob('*.csv');
$dates = array();
for($i=0;$i<count($files);$i++){
    $str = substr($files[$i],-14, -4);
    $dates = array_push($dates, $str);
}
var_dump($dates);

Thanks in advance!


Solution

  • It's either

    array_push($dates, $str);
    

    or

    $dates[] = $str;
    

    Not

    $dates = array_push($dates, $str);