Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关联索引数组到关联数组_Php_Arrays_Phpexcel - Fatal编程技术网

Php 关联索引数组到关联数组

Php 关联索引数组到关联数组,php,arrays,phpexcel,Php,Arrays,Phpexcel,问题 我有一个数组,它是通过以下命令从PHPExcel返回的 <?php require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php'; $excelFile = "excel/1240.xlsx"; $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $objPHPExcel = $objReader->load($excelFile)

问题

我有一个数组,它是通过以下命令从PHPExcel返回的

<?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);
?>
我想要的是

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
                )
        )
)
阵列名称电影、游戏取自表名,因此数量可以是可变的。第一个子数组将始终包含关键名称,例如电影[0]和游戏[0],并且这些名称的数量可以是可变的。我想我知道我需要做下面的事情,但我不知所措

foreach ($arrayData as $value) {
    foreach ($value as $rowKey => $rowValue) {
        for ($i=0; $i <count($value) ; $i++) { 
            # code to add NAME[n] as keys
        }
    }
}
我已经在这里和其他地方广泛搜索了,如果它是重复的,我将删除它

感谢您的任何意见

我想这样做可以:

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);
编辑:测试并更新了代码,有效…

我认为这样做可以:

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);

编辑:测试并更新代码,有效…

类似的操作应该有效:

$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);

演示:

像这样的东西应该可以工作:

$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);
演示:

您可以使用嵌套的数组映射调用。不知怎的,是这样的:

$result = array_map(
    function ($subarr) {
        $names = array_shift($subarr);
        return array_map(
            function ($el) use ($names) {
                return array_combine($names, $el);
            },
            $subarr
        );
    },
    $array
);
您可以使用嵌套的数组映射调用。不知怎的,是这样的:

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

请参见演示

尝试

$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);
  }

}

请参阅演示

答案可能有点晚,但它看起来更像是您尝试过的解决方案

                     //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
}

答案可能有点晚,但看起来更像是你尝试过的解决方案

                     //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
}

对于给定的示例,效果很好,但当张数或钥匙数增加时,似乎会重复第一个数组,例如[电影]=>数组[0]=>数组[Name]=>肖申克的救赎[Rating]=>99[1]=>数组[Name]=>发条橙色[Rating]=>99[2]=>数组[Name]=>肖申克的救赎[Rating]=>39[3]=>数组[Name]=>发条橙[Rating]=>27在给定的示例中效果很好,但当张数或钥匙数增加时,似乎会重复第一个数组,例如[影片]=>array[0]=>array[Name]=>Shawshank Redemption[Rating]=>99[1]=>array[Name]=>99[2]=>array[Name]=>Shawshank Redemption[Rating]=>39[3]=>Array[Name]=>A Clockwork Orange[Rating]=>27接受这是我能理解的第一个工作答案。接受这是我能理解的第一个工作答案。