M
M
msimrial2015-09-29 14:10:14
PHP
msimrial, 2015-09-29 14:10:14

How to provide a cycle not with a series of numbers, but only some?

$notInResult = [0,1,2,4,5,7,8];
for ($j = 0; $j < $nColumn; $j++) {
$value = $sheet->getCellByColumnAndRow($j, $i)->getValue();
if(!in_array($j,$notInResult)){
echo "$value";
}
}
It is necessary to remove certain columns from excel in the phpExcel library. Implemented in this way, can anyone tell me how to remove the bike?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
PQR, 2015-09-29
@msimrial

You can come up with a dozen ways, but you got a fairly simple, readable and understandable code, I would leave it.
For example, here are the alternatives:

$notInResult = [0,1,2,4,5,7,8];
$all = range(0, $nColumn); //массив всех чисел от 0 до $nColumn
$inResult = array_diff($all, $notInResult); //убираем из массива всех чисел те, которые указаны в $notInResult

foreach ($inResult as $j) {
    echo $sheet->getCellByColumnAndRow($j, $i)->getValue();
}

The second option is very similar to yours, but instead of the slow in_array check, the fast isset check is used
$notInResult = [0 => true, 1 => true, 2 => true, 4 => true, 5 => true, 7 => true, 8 => true]; //номера скрываемых колонок будем хранить в ключах массива, а значениями массива сделаем заглушку true
for ($j = 0; $j < $nColumn; $j++) {
    if (!isset($notInResult[$j])) {
        echo $sheet->getCellByColumnAndRow($j, $i)->getValue();
    }
}

O
Optimus, 2015-09-29
Pyan @marrk2

$excel = array(1,2,3,4,5,6,7,8,9); // Исходный массив
$array = array(1,3,5,7);           // ID на удаление
$c = count($array);

for ($j = 0; $j < $c; $j++) {
    // Удаляем например
    $a = $array[$j];
    unset($excel[$a]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question