Not able to map multiple child in array using core php in excel spreadsheet

17 Views Asked by At

I have excel sheet with list of types and each types has labels and each labels has values in excel I am trying to collect in array as Key & Value so that I can upload in server enter image description here

  • As per attachment we have multiple types and each type has labels dynamic
  • My problem is I am not able to fetch labels values from excel sheet using core php
  • I am looking for solution where I can collect in array and push

I tried below code

`for ($col = 'B'; $col <= 'Z'; $col++) {
        $value = $sheet->getCell($col . '2')->getValue();
        // if($value!=''){
            $keys[] = $value;
        // }
    }
    foreach ($keys as $key) {
        // Find the column index for the current key
        $columnIndex = array_search($key, $keys)+2;
        // Extract 'No', 'Name', and 'Title' from the subsequent row
$no = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex).'3')-    >getValue();
        // for($a=1;$a=$count_team;$a++)
        // {
   $name = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+1).'3')->getValue();
   $name1 = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+2).'3')->getValue();
   $name2 = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+3).'3')->getValue();
        // }
    
    // Build the array structure
    $dataArray = [];
    for ($row = 4; $row <= $totalRows; $row++) {
        $carrierValue = $sheet->getCell('A'.$row)->getValue();
$noValue = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex).$row)->getValue();

$nameValue = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+1).$row)->getValue(); $nameValue1 = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+2).$row)->getValue(); $nameValue2 = $sheet->getCell(Coordinate::stringFromColumnIndex($columnIndex+3).$row)->getValue();

            // Add data to the dataArray
if($noValue!='' && $nameValue!=''  && $key!=$additional[0]){
$dataArray[] = array('carrier'=>$carrierValue,'nos' => $noValue, 'name'=>$name, 'emp' => $nameValue, 'name1'=>$name1, 'emp1' => $nameValue1, 'name2'=>$name2, 'emp2' => $nameValue2 );
    }
if($noValue!='' && $nameValue!=''  && $key==$additional[0]){
  $dataArray[] = array('carrier'=>$carrierValue,'no' => $noValue, 'remark' => $nameValue);
    }
}

        // Assign the array to the result array with the key
        $result[$key] = $dataArray;
        print_r($result);die();
    }`

**My Expectation **

  • Read values of labels and store in array
0

There are 0 best solutions below