Question

Problem

I have an array which is returned from PHPExcel via the following

<?php
    require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
    $excelFile = "excel/1240.xlsx";
    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPExcel = $objReader->load($excelFile);
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $arrayData[$worksheet->getTitle()] = $worksheet->toArray();
    }           
    print_r($arrayData);
?>

This returns:

 Array
    (
        [Films] => Array
            (
                [0] => Array
                    (
                        [0] => Name
                        [1] => Rating
                    )    
                [1] => Array
                    (
                        [0] => Shawshank Redemption
                        [1] => 39
                    )    
                [2] => Array
                    (
                        [0] => A Clockwork Orange
                        [1] => 39
                    )    
            )      

    [Games] => Array
        (
            [0] => Array
                (
                    [0] => Name
                    [1] => Rating
                )    
            [1] => Array
                (
                    [0] => F.E.A.R
                    [1] => 4
                )    
            [2] => Array
                (
                    [0] => World of Warcraft
                    [1] => 6
                )    
        )    
)

What I would like to have is

Array
(
    [Films] => Array
        (
            [0] => Array
                (
                    [Name] => Shawshank Redemption
                    [Rating] => 39
                )
            [1] => Array
                (
                    [Name] => A Clockwork Orange
                    [Rating] => 39
                )
        )

    [Games] => Array
        (
            [0] => Array
                (
                    [Name] => F.E.A.R
                    [Rating] => 4
                )
            [1] => Array
                (
                    [Name] => World of Warcraft
                    [Rating] => 6
                )
        )
)

The arrays names (Films, Games) are taken from the sheet name so the amount can be variable. The first sub-array will always contain the key names e.g. Films[0] and Games[0] and the amount of these can be varible. I (think I) know I will need to do something like below but I'm at a loss.

foreach ($arrayData as $value) {
    foreach ($value as $rowKey => $rowValue) {
        for ($i=0; $i <count($value) ; $i++) { 
            # code to add NAME[n] as keys
        }
    }
}

I have searched extensively here and else where if it is a duplicate I will remove it.

Thanks for any input

Was it helpful?

Solution

Try

$result= array();
foreach($arr as $key=>$value){
  $keys = array_slice($value,0,1);
  $values = array_slice($value,1);
  foreach($values as $val){
    $result[$key][] = array_combine($keys[0],$val);
  }

}

See demo here

OTHER TIPS

You may use nested array_map calls. Somehow like this:

$result = array_map(
    function ($subarr) {
        $names = array_shift($subarr);
        return array_map(
            function ($el) use ($names) {
                return array_combine($names, $el);
            },
            $subarr
        );
    },
    $array
);

Demo

Something like this should work:

$newArray = array();
foreach ($arrayData as $section => $list) {
    $newArray[$section] = array();
    $count = count($list);
    for ($x = 1; $x < $count; $x++) {
        $newArray[$section][] = array_combine($list[0], $list[$x]);
    }
}
unset($arrayData, $section, $x);

Demo: http://ideone.com/ZmnFMM

Probably a little late answer, but it looks more like your tried solution

                     //Films,Games // Row Data
foreach ($arrayData as $type => $value)
{
    $key1 = $value[0][0]; // Get the Name Key
    $key2 = $value[0][1]; // Get the Rating Key 

    $count = count($value) - 1;

    for ($i = 0; $i < $count; $i++)
    {
        /* Get the values from the i+1 row and put it in the ith row, with a set key */
        $arrayData[$type][$i] = array(
            $key1 => $value[$i + 1][0],
            $key2 => $value[$i + 1][1],
        );
    }

    unset($arrayData[$type][$count]);       // Unset the last row since this will be repeated data
}

I think this will do:

foreach($arrayData as $key => $array){
    for($i=0; $i<count($array[0]); $i++){
        $indexes[$i]=$array[0][$i];
    }
    for($i=1; $i<count($array); $i++){
        for($j=0; $j<count($array[$i]); $j++){
            $temp_array[$indexes[$j]]=$array[$i][$j];
        }
        $new_array[$key][]=$temp_array;
    }
}
print_r($new_array);

EDIT: tested and updated the code, works...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top