组合2个数组,其中值成为关键

时间:2014-12-10 05:22:23

标签: php arrays multidimensional-array phpexcel

我有2个数组($ arr1和$ arr2)我需要组合使得第一个数组的值在合并第二个数组后成为键。

基本上$ arr1是我使用过的array_combine()的结果。我想对第三个数组实现相同的操作,但是我无法得到我想要的结果。

$ ARR1

Array(

    [fb1] => Array
             (
                  [0] => "test1"
             )
    [fb2] => Array
             (
                  [0] => "test2"
             )  
)

$ ARR2 [] [] []

Array(

    [0] => Array
             (
                 [0] => Array 
                        (

                          [0] => "A"
                          [1] => "B"
                        )
             )
    [1] => Array
             (
                 [0] => Array
                        (
                          [0] => "C"
                          [1] => "D"
                        )
             )  
)

这是我正在寻找的结果:

Array(

    [fb1] => Array
             (
                 [test1] => Array 
                        (

                          [0] => "A"
                          [1] => "B"
                        )
             )
    [fb2] => Array
             (
                 [test2] => Array
                        (
                          [0] => "C"
                          [1] => "D"
                        )
             )  
)

这是我主要使用的代码:

                    $objReader = PHPExcel_IOFactory::createReader('Excel5');
                    $objReader->setReadDataOnly(true);
                    $objPHPExcel = $objReader->load($filer);

                    $objWorksheet = $objPHPExcel->setActiveSheetIndex(1);
                    $objWorksheet1 = $objPHPExcel->setActiveSheetIndex(3);
                    $objWorksheet2 = $objPHPExcel->setActiveSheetIndex(4);

                    echo "<table cellpadding='0' cellspacing='0' border='0' width='100%' id='tblMain' style='color: #FFFFFF;'>" . "\n";
                    echo '<tr>';


                    /*************************************************/
                    // sheet 0 floor
                    $array_test = array();

                        foreach ($objWorksheet->getRowIterator() as $row) 
                        {


                                $rowIndex = $row->getRowIndex();
                                $cell = $objWorksheet->getCell("A". $rowIndex);
                                $array_test[] = $cell->getCalculatedValue(); 

                        }

                    //print '<pre>'.print_r($array_test,true).'<pre>';
                    /*************************************************/
                    // sheet1


                        foreach ($objWorksheet->getRowIterator() as $row) 
                        {
                            PHPExcel_Calculation::getInstance($objWorksheet->getParent())->clearCalculationCache(); 

                            $rowIndex = $row->getRowIndex();
                            $cell = $objWorksheet->getCell("B". $rowIndex);

                            $array_units[][] = $cell->getCalculatedValue();

                        }

                $array_c = $this->combine_arr($array_test, $array_units);

                    // /*************************************************/
                    //sheet 2


                        foreach ($objWorksheet1->getRowIterator() as $row) 
                        {

                                $rowIndex = $row->getRowIndex();
                                $cell = $objWorksheet->getCell("B". $rowIndex);
                                if($cell->getCalculatedValue() == ""){

                                    $artest[][][] =  "NONE";
                                }
                                else{
                                    $artest[][][] =  $cell->getCalculatedValue();

                                }

                        }


                    // var_dump($array_co);
                    //$slice = array_slice($artest, 0, 35);

                    // echo count($array_c)."<br/>";
                    // echo count($slice);
                    //$array_co = array_merge_recursive(, $slice);

                    // print '<pre>'.print_r($array_c, true).'<pre>';                       
                    // print '<pre>'.print_r($artest, true).'<pre>';


                        $merged = array_merge_recursive($array_c, $artest);
                        print '<pre>'.print_r($array_c, true).'<pre>';
                        echo "<br/>";
                        print '<pre>'.print_r($artest, true).'<pre>';
                        echo "<br/>";
                        print '<pre>'.print_r($merged, true).'<pre>';

1 个答案:

答案 0 :(得分:0)

$newArr = array(); foreach($arr2 as $tk => $tv){
    foreach($arr1 as $lk => $lv){
        $newArr[$lk][$lv[0]] = $tv[0];
    }
}