1

i have a csv file with data shown in the picture.What i want to do is,read the csv file and make a multidimensional array given below

Array(
     array(
           [ZAIN BUSINESS EMPIRE PVT LTD]=>['10684054','10686738','10686688','10684062']

          ),

     );

I have read the csv and stored the name of companies and ids in separate arrays.

  $file_name=$_FILES['file_upload']['tmp_name'];
  $csv = array_map("str_getcsv", file($file_name,FILE_SKIP_EMPTY_LINES));
  $keys = array_shift($csv);
   foreach ($csv as $i=>$row) 
     {
     $csv[$i] = array_combine($keys, $row);
     }
     foreach ($csv as $data)
     {
       $company_id_arr[]=$data['Customer_ID'];
       $name[]=$data['STANDARD NAME IN M2M DB'];
     }

enter image description here

9
  • add your sample csv data with this code Commented Feb 2, 2018 at 8:46
  • @Priyank sir uploaded Commented Feb 2, 2018 at 8:48
  • why only ['10684054','10686738','10686688','10684062']? what about other id's.Also what's the column-name for these entries? Commented Feb 2, 2018 at 8:53
  • Why have the extra Array() around it all? Or in other words, I don't understand the result you want. It's unclear. Commented Feb 2, 2018 at 8:55
  • what is the name of third column ( column C) ? Commented Feb 2, 2018 at 8:56

2 Answers 2

2

<?php
$file_name=$_FILES['file_upload']['tmp_name'];
$csv = array_map("str_getcsv", file($file_name,FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);
 foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
   }
   foreach ($csv as $data){
 $company_id_arr[$data['STANDARD NAME IN M2M DB']][]=$data['Customer_ID'];
    //$name[]=$data['STANDARD NAME IN M2M DB'];

    
   }

?>

Sign up to request clarification or add additional context in comments.

Comments

2

The solution is:

// your result array
$result = [];
foreach ($csv as $data)
{
    // check if your key not exists in $result
    if (empty($result[$data['STANDARD NAME IN M2M DB']])) {
        $result[$data['STANDARD NAME IN M2M DB']] = [];
    }

    // Add value under required key
    $result[$data['STANDARD NAME IN M2M DB']][] = $data['Customer_ID'];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.