Slice数组然后将内容合并为php

时间:2015-05-01 09:31:43

标签: php arrays phpexcel

我的数组结果返回许多不同的数组,我想根据数字x切割数组,然后结合结果给我一个完整的结果..例如,假设数组的大小为8我想切片它然后将数组项组合成2个最终数组......

我的代码:

$number = $objPHPExcel->getSheetCount(); //0 - nth in this case n=2

for ($i = 0; $i <= $number - 1; $i++) {
    $objPHPExcel->setActiveSheetIndex($i);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $sheet_name = $objWorksheet->getTitle();
    $value = $objWorksheet->getCell('C19')->getValue();
    $value2 = $this->getTestNumberC($value); //get db row count and data for each row

    if (!empty($value) && (int) $value == $value2[0]->wk_no) {
        for($j=0;$j<count($value2);$j++){
            $diss_array = array(
                'component' => $sheet_name,
                $value2[$j]->parameter => $objWorksheet->getCell($value2[$j]->cell)->getFormattedValue()
            );
            // $new = array_slice($diss_array,0,3);
            print "<pre>";
            print_r($diss_array);
            print "</pre>";
        } else if(/*some condition*/){
    }else{
   }
}



function getTestNumberC($v){
    return $this->db->where('wk_no',$v)->get('generic_worksheet')->result();
}

我有什么

Array
(
    [component] => eSORAL 40 iNJECTION
    [Average] => 102.52
)

Array
(
    [component] => eSORAL 40 iNJECTION
    [RSD] => 0.26%
)

Array
(
    [component] => eSORAL 40 iNJECTION
    [n] => 3
)

Array
(
    [component] => eSORAL 40 iNJECTION (2)
    [Average] => 135.00
)

Array
(
    [component] => eSORAL 40 iNJECTION (2)
    [RSD] => 0.19%
)

Array
(
    [component] => eSORAL 40 iNJECTION (2)
    [n] => 3
)

我想做的就是有这样的事情

 Array
    (
        [component] => eSORAL 40 iNJECTION
        [Average] => 102.52
        [RSD] => 0.26%
        [n] => 3
    )

 Array
    (
        [component] => eSORAL 40 iNJECTION (2)
        [Average] => 135.00
        [RSD] => 0.19%
        [n] => 3
    )

我试图用array_slice切片($ array,0,3)结果是一样的。任何建议!

2 个答案:

答案 0 :(得分:1)

是的,我想我明白你在这里要做什么。试试这个:

// 0 - nth in this case n=2
$number       = $objPHPExcel->getSheetCount();
$newDissArray = array();

for ($i = 0; $i <= $number - 1; $i++) {
    $objPHPExcel->setActiveSheetIndex($i);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $sheet_name   = $objWorksheet->getTitle();
    $value        = $objWorksheet->getCell('C19')->getValue();
    //get db row count and data for each row
    $value2       = $this->getTestNumberC($value);

    if (!empty($value) && (int) $value == $value2[0]->wk_no) {
        for ($j=0; $j < count($value2); $j++) {
            // as we loop through we use the sheet name as a key to
            // build up an associative array of all the components
            if (!isset($newDissArray[$sheet_name])) {
                $newDissArray[$sheet_name] = array(
                    'component' => $sheet_name
                );
            }

            $newDissArray[$sheet_name][$value2[$j]->parameter]
                = $objWorksheet->getCell($value2[$j]->cell)->getFormattedValue();
        }

        // $newDissArray is at this point an associative array
        // so we need to strip it down to values only
        $newDissArray = array_values($newDissArray);
    }
}

print "<pre>";
print_r($newDissArray);
print "</pre>";


function getTestNumberC($v){
    return $this->db->where('wk_no',$v)->get('generic_worksheet')->result();
}

答案 1 :(得分:0)

你知道每个数组只有两个值,所以你可以使用for循环来实现它!

$array1 = array('component' => 'eSORAL 40 iNJECTION', 'Average' => '102.52');
$array2 = array('component' => 'eSORAL 40 iNJECTION', 'RSD' => '0.26%');
$array2 = array('component' => 'eSORAL 40 iNJECTION', 'n' => '3');
$combine = array();

for($i = 0, $i < 2; $++) {
    $combine = array(
         'component' => $array1['component'],
         'Average' => $array1['Average'],
         'RSD' => $array2['RSD'],
         'n' => $array3['n'],
    );
}

print_r($combine);