mmr
mmr

Reputation: 516

Combine 2 arrays where the value becomes the key

I have 2 arrays ($arr1 and $arr2) I needed to combine such that the values from the first array becomes the key after the second array has been merged.

Basically $arr1 is a result of array_combine() I have used. I wanted to implement the same action to the 3rd array however, i cannot get the result I wanted.

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

This is the result I'm looking for:

Array(

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

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

This is the code I mainly used:

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

Upvotes: 0

Views: 73

Answers (1)

Atul
Atul

Reputation: 27

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

Upvotes: 0

Related Questions