0

I am trying to add two arrays $arraydata1 and $arraydata2 to get the array $result. But it shows the result as if $result=$arraydata1;, not as $result=$arraydata1+$arraydata2;.

Here is the array I am trying to add after getting it from the CSV file and storing it in different array type variable.

$result = array();
$arraydata1 = array();
$arraydata2 = array();
$key1 = array("itemid","itembrand","itemname","itemmodel","itemdesc","itemtype","itemprice","itemimgfolder","itemimage");
$key2 = array("itemid","itembrand","itemname","itemmodel","itemdesc","itemtype","itemprice","itemimgfolder","itemimage");
foreach (file('data/dell/data.csv') as $key => $str)
{
    if ($key == 0) continue;  // to skip headings of the csv file
    $values = str_getcsv($str, "\t", '', '');
    $arraydata1[] = array_combine($key1, $values);
} // to store first array data

foreach (file('data/hp/data.csv') as $key => $str)
{
    if ($key == 0) continue;  // to skip headings of the csv file
    $values = str_getcsv($str, "\t", '', '');
    $arraydata2[] = array_combine($key2, $values);
}

$result = $arraydata2+$arraydata2; // result the combination of the two arrays

Everything else is working fine. I have also tried array_combine() but it did not work either.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Vishal Kumar Sahu
  • 1,232
  • 3
  • 15
  • 27

1 Answers1

1

As the possible duplicate comment suggests, I think the answer is to use use array_merge rather than +, but why not just store them all in the same array to begin with?

$keys = array("itemid","itembrand","itemname","itemmodel","itemdesc","itemtype","itemprice","itemimgfolder","itemimage");

$files = array('data/dell/data.csv', 'data/hp/data.csv');

foreach ($files as $file) {
    foreach (file($file) as $key => $str) {
        if ($key == 0) continue;  // to skip headings of the csv file
        $values = str_getcsv($str, "\t", '', '');
        $result[] = array_combine($keys, $values);
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80